167 lines
3.7 KiB
C
167 lines
3.7 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include <math.h>
|
|
|
|
#include <raylib.h>
|
|
#include <raymath.h>
|
|
|
|
#include "types.h"
|
|
#include "game.h"
|
|
#include "draw.h"
|
|
#include "ui.h"
|
|
|
|
GUIInfo gui_info;
|
|
|
|
void main_menu_draw(Widget *widget, Game *game){
|
|
DrawRectangleRec((Rectangle){.x = 0, .y = 0, .width = SCREEN_W, .height = SCREEN_H}
|
|
, WHITE);
|
|
}
|
|
|
|
void main_menu_do_event(Widget *widget, Game *game, MTEvent event){
|
|
if(event.type == EV_Mouse && event.click == MOUSE_BUTTON_LEFT){
|
|
gui_info.widget_active[0] = true;
|
|
gui_info.widget_active[1] = false;
|
|
game->paused = false;
|
|
}
|
|
}
|
|
|
|
void widget_draw_game(Widget *widget, Game *game){
|
|
draw_game(game);
|
|
}
|
|
|
|
Widget main_menu = {
|
|
.box = {.x = 0, .y = 0, .width = -1, .height = -1},
|
|
.draw = &main_menu_draw,
|
|
.do_event = &main_menu_do_event,
|
|
.capt_flags = CF_Mouse | CF_Keyb,
|
|
.buttons = {}
|
|
};
|
|
|
|
Widget game_widget = {
|
|
.box = {0,0,0,0},
|
|
.draw = &widget_draw_game,
|
|
.do_event = NULL,
|
|
.capt_flags = 0,
|
|
.buttons = {}
|
|
};
|
|
|
|
void init_ui(){
|
|
gui_info = (GUIInfo){{&game_widget, &main_menu}, {false, true}, NULL};
|
|
}
|
|
|
|
void draw_widgets(Game *game){
|
|
for(int i = 0; i < WIDGET_N; i++){
|
|
if(!gui_info.widget_active[i])
|
|
continue;
|
|
Widget *w = gui_info.widgets[i];
|
|
if(w->draw)
|
|
w->draw(w, game);
|
|
}
|
|
}
|
|
|
|
bool widget_coll(Rectangle widget, V2d point){
|
|
if(widget.width < 0)
|
|
widget.width = SCREEN_W;
|
|
if(widget.height < 0)
|
|
widget.height = SCREEN_H;
|
|
return CheckCollisionPointRec(point, widget);
|
|
}
|
|
|
|
bool widgets_treat_event(Game *game, MTEvent event){
|
|
bool treated = true;
|
|
for(int i = 0; i < WIDGET_N; i++){
|
|
if(!gui_info.widget_active[i])
|
|
continue;
|
|
Widget *w = gui_info.widgets[i];
|
|
if(!w || !w->do_event)
|
|
continue;
|
|
if(!widget_coll(w->box, event.pos))
|
|
continue;
|
|
// Only actually call do_key if the event type matches event flags
|
|
switch(event.type){
|
|
default:
|
|
continue;
|
|
case EV_Mouse:
|
|
if(w->capt_flags & ~CF_Mouse)
|
|
break;
|
|
continue;
|
|
case EV_Keyb:
|
|
if(w->capt_flags & ~CF_Keyb)
|
|
break;
|
|
continue;
|
|
}
|
|
treated = false;
|
|
w->do_event(w, game, event);
|
|
}
|
|
return treated;
|
|
}
|
|
|
|
bool ui_do_key(Game *game, int key){
|
|
MTEvent event = {.type = EV_Keyb, {.key = key}};
|
|
return widgets_treat_event(game, event);
|
|
}
|
|
|
|
bool ui_do_mouse(Game *game, V2d pos, int click){
|
|
MTEvent event = {.type = EV_Mouse, {.click = click}, .pos = pos};
|
|
return widgets_treat_event(game, event);
|
|
}
|
|
|
|
void try_interface(Game *game, V2d pos){
|
|
|
|
DrawRectangleV(pos, (V2d){16,16},PINK);
|
|
|
|
Machine *mach = get_machine_from_pos(&game->machines, pos);
|
|
|
|
if(mach){
|
|
printf("found machine %d\n",mach->id);
|
|
return;
|
|
}
|
|
|
|
Worker *work = get_worker_from_pos(&game->workers, pos);
|
|
|
|
if(!work)
|
|
return;
|
|
|
|
printf("found worker\n");
|
|
|
|
return;
|
|
}
|
|
|
|
bool keydown(int key){
|
|
return IsKeyDown(key);
|
|
}
|
|
|
|
int get_mouse(){
|
|
for(int i = 0; i < MOUSE_BUTTON_NULL; i++){
|
|
if(IsMouseButtonPressed(i))
|
|
return i;
|
|
}
|
|
return MOUSE_BUTTON_NULL;
|
|
}
|
|
|
|
void get_keys(Game *game){
|
|
V2d mouse_pos = GetMousePosition();
|
|
int mouse_click = get_mouse();
|
|
if(ui_do_mouse(game, mouse_pos, mouse_click)){
|
|
if(mouse_click == MOUSE_BUTTON_LEFT)
|
|
try_interface(game, GetScreenToWorld2D(mouse_pos,game->camera));
|
|
}
|
|
|
|
if(keydown(KEY_W))
|
|
game->camera.target.y -= 3;
|
|
if(keydown(KEY_S))
|
|
game->camera.target.y += 3;
|
|
if(keydown(KEY_A))
|
|
game->camera.target.x -= 3;
|
|
if(keydown(KEY_D))
|
|
game->camera.target.x += 3;
|
|
if(keydown(KEY_UP))
|
|
game->camera.zoom *= 1.1;
|
|
if(keydown(KEY_DOWN))
|
|
game->camera.zoom *= 1/1.1;
|
|
if(IsKeyPressed(KEY_P))
|
|
game->paused = !game->paused;
|
|
}
|