00001 #include "editor.h"
00002
00003 #define PROPERTIES_X 10
00004 #define PROPERTIES_Y 20
00005 #define PROPERTIES_WIDTH 140
00006 #define PROPERTIES_HEIGHT 70
00007 #define LABEL_WIDTH 35
00008
00009 #define PROPERTY_MAX_LENGTH 64
00010 char property_name[PROPERTY_MAX_LENGTH];
00011 char property_author[PROPERTY_MAX_LENGTH];
00012 char property_hint[PROPERTY_MAX_LENGTH];
00013 char property_congrat[PROPERTY_MAX_LENGTH];
00014
00015 #define NUM_WIDGETS 4
00016 struct widget widgets[NUM_WIDGETS] = {
00017 { 2, 14, 135, 9, 0, draw_textbox, key_textbox, "Name", property_name },
00018 { 2, 24, 135, 9, 0, draw_textbox, key_textbox, "Author", property_author},
00019 { 2, 34, 135, 9, 0, draw_textbox, key_textbox, "Hint", property_hint },
00020 { 2, 44, 135, 9, 0, draw_textbox, key_textbox, "Congrats",property_congrat},
00021 };
00022 int current_widget = 0;
00023
00024
00025 void enter_properties_state(void)
00026 {
00027 input_state = properties_state;
00028 strcpy(property_name, current_map->name);
00029 strcpy(property_author, current_map->author);
00030 strcpy(property_hint, current_map->hint);
00031 strcpy(property_congrat, current_map->congrat);
00032 draw_current_map();
00033 draw_rect(PROPERTIES_X, PROPERTIES_Y, PROPERTIES_WIDTH, PROPERTIES_HEIGHT);
00034 draw_string("Map properties", PROPERTIES_X + 3, PROPERTIES_Y + 2);
00035 draw_line(PROPERTIES_X, PROPERTIES_Y+8,
00036 PROPERTIES_X+PROPERTIES_WIDTH-1, PROPERTIES_Y+8, A_NORMAL);
00037 current_widget = 0;
00038 widgets[0].focused = 1;
00039 redraw_properties();
00040 }
00041
00042
00043 void redraw_properties(void)
00044 {
00045 int ii;
00046 for(ii=0; ii<NUM_WIDGETS; ii++)
00047 widgets[ii].draw(&widgets[ii]);
00048 }
00049
00050
00051 void properties_state(int key)
00052 {
00053 if(key == KEY_ENTER) {
00054 apply_property_changes();
00055 widgets[current_widget].focused = 0;
00056 enter_edit_state();
00057 } else if(key == KEY_ESC) {
00058 widgets[current_widget].focused = 0;
00059 enter_edit_state();
00060 } else if(key == KEY_UP) {
00061 if(current_widget > 0)
00062 switch_focus(current_widget-1);
00063 else
00064 switch_focus(NUM_WIDGETS-1);
00065 } else if(key == KEY_DOWN) {
00066 switch_focus((current_widget+1) % NUM_WIDGETS);
00067 } else {
00068 widgets[current_widget].key(&widgets[current_widget], key);
00069 }
00070 }
00071
00072
00073 void apply_property_changes(void)
00074 {
00075 strcpy(current_map->name, property_name);
00076 strcpy(current_map->author, property_author);
00077 strcpy(current_map->hint, property_hint);
00078 strcpy(current_map->congrat, property_congrat);
00079 }
00080
00081
00082
00083 void switch_focus(int focus)
00084 {
00085 widgets[current_widget].focused = 0;
00086 widgets[current_widget].draw( &widgets[current_widget] );
00087 current_widget = focus;
00088 widgets[current_widget].focused = 1;
00089 widgets[current_widget].draw( &widgets[current_widget] );
00090 }
00091
00092
00093 void key_textbox(struct widget *this, int key)
00094 {
00095 char *str = (char*)this->content;
00096
00097 if(key == KEY_LEFT || key == KEY_BACKSPACE)
00098 {
00099 if(strlen(str) > 0)
00100 str[ strlen(str)-1 ] = '\0';
00101 } else if( key < 256 ) {
00102 if(strlen(str)+1 < PROPERTY_MAX_LENGTH) {
00103 str[ strlen(str)+1 ] = '\0';
00104 str[ strlen(str) ] = key;
00105 }
00106 }
00107 draw_textbox(this);
00108 }
00109
00110
00111 void draw_textbox(struct widget *this)
00112 {
00113 clear_rect(PROPERTIES_X+this->x, PROPERTIES_Y+this->y,
00114 this->width, this->height);
00115 draw_rect(PROPERTIES_X+this->x+LABEL_WIDTH, PROPERTIES_Y+this->y,
00116 this->width-LABEL_WIDTH, this->height);
00117 draw_string(this->label, this->x+PROPERTIES_X+2, this->y+PROPERTIES_Y+2);
00118 draw_string((const char*)(this->content),
00119 this->x+PROPERTIES_X+2+LABEL_WIDTH, this->y+PROPERTIES_Y+2);
00120 if(this->focused) {
00121 xor_rect(PROPERTIES_X+this->x+LABEL_WIDTH+1, PROPERTIES_Y+this->y+1,
00122 this->width-LABEL_WIDTH-2, this->height-2);
00123 }
00124 }
00125
00126