Skip to content

Commit

Permalink
Add gui tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skaller committed Jan 31, 2015
1 parent 304f20d commit 21aaa3b
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 31 deletions.
36 changes: 22 additions & 14 deletions demos/sdl/tgui.flx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FlxGui::init();
// get a font, and the recommended vertical
// spacing between lines
var font_name = System::argv 1;
if font_name == "" do font_name = OSX_dflt_font(); done
if font_name == "" do font_name = dflt_mono_font(); done

var font : font_t = get_font(font_name, 12);
var lineskip = get_lineskip font;
Expand Down Expand Up @@ -48,6 +48,7 @@ var bsmover = (label_colour=red, bg_colour=yellow,top_colour=blue,


proc mkbutton (w:window_t, x:int, y:int, label:string, font:font_t,
oresp: oschannel[button_action_t],
pbd: &button_display_t,
pos: &oschannel[event_t]
)
Expand All @@ -56,7 +57,7 @@ proc mkbutton (w:window_t, x:int, y:int, label:string, font:font_t,
var bd = ButtonDisplay (bm) (w, font, label, bsup, bsdown, bsdis, bsmover,
x,y, x+100,y+20,x+5,y+15);
var ich,och = mk_ioschannel_pair[event_t]();
spawn_fthread$ button_controller (bm, bd, ich);
spawn_fthread$ button_controller (bm, bd, ich, oresp);
pbd <- bd ;
pos <- och;
}
Expand All @@ -67,7 +68,8 @@ proc mkwin
(
x:int, y:int, pw: &window_t,
pf: &varray[oschannel[event_t]],
pbuttons: &varray[oschannel[event_t]]
pbuttons: &varray[oschannel[event_t] * ischannel[button_action_t]],
iaction:ischannel[button_action_t], oaction:oschannel[button_action_t]
)
{
var w = window_t("My Window",x,y,400,400);
Expand All @@ -85,38 +87,42 @@ proc mkwin
var ch1: oschannel[event_t]; mk_field (w, 50, 60, font, black, white, "EDITFIELD ONE", &ch1);
var ch2: oschannel[event_t]; mk_field (w, 50, 120, font, black, white, "EDITFIELD TWO", &ch2);
var ch3: oschannel[event_t]; mk_field (w, 50, 180, font, black, white, "EDITFIELD THREE", &ch3);
var b1 : button_display_t; var bch1: oschannel[event_t]; mkbutton (w,50,250,"hello",font,&b1, &bch1);
var b2 : button_display_t; var bch2: oschannel[event_t]; mkbutton (w,50,300,"hello2",font,&b2, &bch2);
var b1 : button_display_t; var bch1: oschannel[event_t];
mkbutton (w,50,250,"hello",font,oaction,&b1, &bch1);
var b2 : button_display_t; var bch2: oschannel[event_t];
mkbutton (w,50,300,"hello2",font,oaction,&b2, &bch2);

w.update;
var fields = varray[oschannel[event_t]] (ch1, ch2, ch3);
pw <- w; pf <- fields;
var buttons = varray[oschannel[event_t]] (bch1, bch2);
var buttons = varray[oschannel[event_t] * ischannel[button_action_t]] ((bch1,iaction), (bch2,iaction));
pbuttons <- buttons;
}
// create a window manager
var wm = window_manager();

begin
var iaction,oaction = #mk_ioschannel_pair[button_action_t];
var w1: window_t;
var flds1: varray[oschannel[event_t]];
var buts1: varray[oschannel[event_t]];
mkwin(150,150, &w1, &flds1, &buts1);
var buts1: varray[oschannel[event_t] * ischannel[button_action_t]];
mkwin(150,150, &w1, &flds1, &buts1, iaction, oaction);

var w2: window_t;
var flds2: varray[oschannel[event_t]];
var buts2: varray[oschannel[event_t]];
mkwin(200,200, &w2, &flds2, &buts2);
var buts2: varray[oschannel[event_t] * ischannel[button_action_t]];
mkwin(200,200, &w2, &flds2, &buts2,iaction, oaction);

var w3: window_t;
var flds3: varray[oschannel[event_t]];
var buts3: varray[oschannel[event_t]];
mkwin(250,250, &w3, &flds3, &buts3);
var buts3: varray[oschannel[event_t] * ischannel[button_action_t]];
mkwin(250,250, &w3, &flds3, &buts3, iaction, oaction);

// make an event handler for our window
proc ehandler
(
fields: varray[oschannel[event_t]],
buts: varray[oschannel[event_t]]
buts: varray[oschannel[event_t] * ischannel[button_action_t]]
)
(input:ischannel[event_t]) ()
{
Expand All @@ -142,7 +148,9 @@ begin
done
write$ fields.selected_field, ev;
for but in buts do
write$ but,ev;
write$ but.0,ev;
var result = read$ but.1;
C_hack::ignore (result);
done
run = ev.window.event.SDL_WindowEventID != SDL_WINDOWEVENT_CLOSE ;
done
Expand Down
18 changes: 14 additions & 4 deletions src/lib/gui/button.flx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class FlxGuiButton
| Mouseover // read and mouse is over
;

union button_action_t =
| NoAction
| ClickAction
;

interface button_model_t
{
get_state: 1 -> button_state_t;
Expand Down Expand Up @@ -91,7 +96,8 @@ proc button_controller
(
bm: button_model_t,
bd: button_display_t,
ec:ischannel[event_t]
ec:ischannel[event_t],
response:oschannel[button_action_t]
) () =
{
bd.display();
Expand All @@ -114,33 +120,37 @@ proc button_controller
| _ => ;
endmatch;
done
write$ response, NoAction;

| $(SDL_MOUSEBUTTONDOWN) =>
x,y = e.button.x,e.button.y; //int32
if SDL_Point (x.int,y.int) \in bd.get_client_rect () do
//println$ "Button down in client rect of button " + bd.get_label();
bm.set_state Down; bd.display();
done
write$ response, NoAction;

| $(SDL_MOUSEBUTTONUP) =>
x,y = e.button.x,e.button.y; //int32
if SDL_Point (x.int,y.int) \in bd.get_client_rect () do
//println$ "Button up in client rect of button " + bd.get_label();
bm.set_state Mouseover; bd.display();
write$ response, ClickAction;
else
bm.set_state Up; bd.display();
write$ response, NoAction;
done
| $(SDL_WINDOWEVENT) when e.window.event == SDL_WINDOWEVENT_LEAVE.uint8 =>
bm.set_state Up; bd.display();
write$ response, NoAction;

| _ => ;
| _ =>
write$ response, NoAction;
endmatch;
e = read ec;
done

}




} // class
6 changes: 5 additions & 1 deletion src/lib/gui/font.flx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class FlxGuiFont
{
fun OSX_dflt_font() => "/Library/Fonts/Courier New.ttf";
private fun / (s:string, t:string) => Filename::join (s,t);

fun dflt_mono_font() => #Config::std_config.FLX_SHARE_DIR/ "src"/"lib"/"fonts"/ "Courier New.ttf";
fun dflt_sans_serif_font() => #Config::std_config.FLX_SHARE_DIR/ "src"/"lib"/"fonts"/ "Arial.ttf";
fun dflt_serif_font() => #Config::std_config.FLX_SHARE_DIR/ "src"/"lib"/"fonts"/ "Times New Roman.ttf";

gen get_font (font_file:string, ptsize:int) = {
var font = TTF_OpenFont (font_file,ptsize);
Expand Down
2 changes: 0 additions & 2 deletions src/lib/std.flx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ proc _swap[t]: &t * &t =
"swapper($1,$2);"
requires swapper[t];

publish "Universal type 'x as x'"
typedef any = any;

include "std/__init__";
1 change: 0 additions & 1 deletion src/lib/std/algebraic.flx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

//$ Additive symmetric float-approximate group, symbol +.
//$ Note: associativity is not assumed.
class FloatAddgrp[t] {
Expand Down
1 change: 1 addition & 0 deletions src/lib/std/flx_tclass.flx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
typedef void = 0;
typedef unit = 1;
typedef bool = 2;
typedef any = any;

// -----------------------------------------------------------------------------

Expand Down
1 change: 0 additions & 1 deletion src/lib/std/order.flx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// equality: technically, equivalence relation
class Eq[t] {
virtual fun == : t * t -> bool;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/flx_build_prep.flx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FlxPrepBuild
if cmd.repo != cmd.target_dir/'share' do
println$ "Copy repository "+cmd.repo/'src -> ' + cmd.target_dir/'share'/'src';
CopyFiles::copyfiles(cmd.repo/'src',
'(.*\.(h|hpp|ml|mli|c|cpp|cxx|cc|flx|flxh|fdoc|fsyn|js|html|css|svg|png|gif|jpg|files|include))',
'(.*\.(h|hpp|ml|mli|c|cpp|cxx|cc|flx|flxh|fdoc|fsyn|js|html|css|svg|png|gif|jpg|files|include|ttf))',
cmd.target_dir/'share'/'src'/'${1}',true,cmd.debug);
else
println$ "Cannot copy repo because source = target";
Expand Down
2 changes: 1 addition & 1 deletion src/web/tutopt/sdlgui/gui_02_font_01.flx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FlxGui::init();
var w = window_t("Felix:gu_02_font_01",100,100,400,400);
w.clear lightgrey;

var font_name = OSX_dflt_font(); // temporary HACK!
var font_name = dflt_sans_serif_font();
var font : font_t = get_font(font_name, 16);
var lineskip = get_lineskip font;
w.write (100,100,font,black,"Felix says: ");
Expand Down
21 changes: 17 additions & 4 deletions src/web/tutopt/sdlgui/gui_03_draw_01.flx
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
include "gui/__init__";
open FlxGui;

println$ "Basic Line Drawing Test";
println$ "Basic Drawing Test";

var clock = Faio::mk_alarm_clock();

// Initialise the SDL system (SDL + TTF + IMAGE)
FlxGui::init();

var w = window_t("Felix:gui_03_draw_01",100,100,400,400);
var w = window_t("Felix:gui_03_draw_01",100,100,400,600);
w.clear lightgrey;

var font_name = OSX_dflt_font(); // temporary HACK!
var font_name = dflt_sans_serif_font();
var font : font_t = get_font(font_name, 12);
var lineskip = get_lineskip font;
w.write (100,100,font,black,"Basic Line Drawing Test");
w.write (10,10,font,black,"Basic Drawing Test");

fun / (x:string, y:string) => Filename::join (x,y);
var imgfile = #Config::std_config.FLX_SHARE_DIR / "src" / "web" / "images" / "FelixWork.jpg";

var ppic : &drawing_surface_t =
IMG_Load (C_hack::cast[&char] imgfile.cstr)
;

var dst = rect_t (20,20,0,0);
var result = SDL_BlitSurface (ppic, C_hack::null[rect_t], SDL_GetWindowSurface w,&dst);
println$ "Blit result " + result.str;
w.update();

w.draw_line (RGB(0,0,255), 100,110,200,110);
w.draw_line (RGB(0,0,255), 100,210,200,210);
w.draw_line (RGB(0,0,255), 100,110,100,210);
Expand Down
2 changes: 1 addition & 1 deletion src/web/tutopt/sdlgui/gui_04_wm_01.flx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FlxGui::init();
var w = window_t("Felix:gui_04_wm_01",100,100,400,400);
w.clear lightgrey;

var font_name = OSX_dflt_font(); // temporary HACK!
var font_name = dflt_sans_serif_font();
var font : font_t = get_font(font_name, 12);
var lineskip = get_lineskip font;

Expand Down
2 changes: 1 addition & 1 deletion src/web/tutopt/sdlgui/gui_05_window_controller_01.flx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var clock = Faio::mk_alarm_clock();
// Initialise the SDL system (SDL + TTF + IMAGE)
FlxGui::init();

var font_name = OSX_dflt_font(); // temporary HACK!
var font_name = dflt_sans_serif_font();
var font : font_t = get_font(font_name, 12);
var lineskip = get_lineskip font;

Expand Down
Loading

0 comments on commit 21aaa3b

Please sign in to comment.