include stdio include Ewl include string void destroy_cb Ewl_Widget vo

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <Ewl.h>
#include <string>
void destroy_cb(Ewl_Widget *w, void *event, void *data) {
ewl_widget_destroy(w);
ewl_main_quit();
}
Ewl_Widget *hboxes[4];
Ewl_Widget *buttons[4][4];
Ewl_Widget *emptybox;
int Mask[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int iS,jS;
void switch_buttons(int m, int s)
{
if(jS+Mask[m][1]<0||iS+Mask[m][0]<0||jS+Mask[m][1]>3||iS+Mask[m][0]>3)return;
ewl_container_child_remove(EWL_CONTAINER(hboxes[iS]), buttons[iS+Mask[m][0]][jS+Mask[m][1]]);
if(!s){
ewl_container_child_insert(EWL_CONTAINER(hboxes[iS]), buttons[iS+Mask[m][0]][jS+Mask[m][1]],jS);
ewl_container_child_insert(EWL_CONTAINER(hboxes[iS+Mask[m][0]]), emptybox,jS+Mask[m][1]);
}
buttons[iS][jS]=buttons[iS+Mask[m][0]][jS+Mask[m][1]];
buttons[iS+Mask[m][0]][jS+Mask[m][1]]=NULL;
iS+=Mask[m][0];
jS+=Mask[m][1];
}
void key_up_cb(Ewl_Widget *w, void *event, void *data)
{
//XK_Escape
Ewl_Event_Key_Up *e = (Ewl_Event_Key_Up *)event;
int i, j;
for(i = 0; i<4; i++)
for(j = 0; j<4; j++)
if(buttons[i][j]==NULL)
{
iS=i;
jS=j;
i=5;break;
}
switch(e->base.keyname[0])
{
printf("aaa");
case 'w':
switch_buttons(0,0);
break;
case 'a':
switch_buttons(1,0);
break;
case 's':
switch_buttons(2,0);
break;
case 'd':
switch_buttons(3,0);
break;
case 'q':
for(i=0;i<1000;i++)
switch_buttons(rand()%4, 1);
for(i = 0; i<4; i++)
for(j = 0; j<4; j++)
if(!buttons[i][j])
ewl_container_child_append(EWL_CONTAINER(hboxes[i]), emptybox);
else
ewl_container_child_append(EWL_CONTAINER(hboxes[i]), buttons[i][j]);
break;
default: return;
}
//printf("iS=%d jS=%d iD=%d jD=%d\n\n",iS,jS,iD,jD);
}
int main(int argc, char ** argv)
{
if (!ewl_init(&argc, argv))
{
printf("Unable to init ewl\n");
return 1;
}
Ewl_Widget *win = ewl_window_new();
ewl_window_title_set(EWL_WINDOW(win), "Пятнашки");
ewl_window_name_set(EWL_WINDOW(win), "EWL_WINDOW");
ewl_window_class_set(EWL_WINDOW(win), "EWLWindow");
ewl_object_size_request(EWL_OBJECT(win), 600, 800);
ewl_callback_append(win, EWL_CALLBACK_DELETE_WINDOW, destroy_cb, NULL);
ewl_callback_append(win, EWL_CALLBACK_KEY_UP, key_up_cb, NULL);
ewl_widget_show(win);
Ewl_Widget *mainvbox = ewl_vbox_new();
ewl_container_child_append(EWL_CONTAINER(win), mainvbox);
ewl_box_spacing_set(EWL_BOX(mainvbox), 10);
ewl_object_padding_set(EWL_OBJECT(mainvbox), 10, 10, 10, 190);
ewl_widget_show(mainvbox);
int i, j;
char buf[BUFSIZ];
for(i = 0; i<4; i++)
{
hboxes[i] = ewl_hbox_new();
ewl_container_child_append(EWL_CONTAINER(mainvbox), hboxes[i]);
ewl_box_spacing_set(EWL_BOX(hboxes[i]), 10);
ewl_widget_show(hboxes[i]);
for(j = 0; j<4; j++)
{
if(i==3&&j==3)buttons[i][j]=NULL;
else{
buttons[i][j] = ewl_button_new();
ewl_container_child_append(EWL_CONTAINER(hboxes[i]), buttons[i][j]);
ewl_object_alignment_set(EWL_OBJECT(buttons[i][j]), EWL_FLAG_ALIGN_CENTER);
ewl_widget_disable(buttons[i][j]);
ewl_widget_show(buttons[i][j]);
snprintf(buf, BUFSIZ,"%d",i*4+1+j);
ewl_button_label_set(EWL_BUTTON(buttons[i][j]), buf);
}
}
}
emptybox=ewl_box_new();
ewl_container_child_append(EWL_CONTAINER(hboxes[3]), emptybox);
ewl_widget_show(emptybox);
ewl_main();
return 0;
}