Esteu aquí: Inici Assignatures Programació Concurrent i en Temps Real Pràctiques mòdul sporta

mòdul sporta

sporta.erl — Erlang source code, 2 kB (2098 bytes)

Continguts del fitxer

-module(sporta).
-include_lib("wx/include/wx.hrl").
-export([new/0, pos_porta/1, kill/0, porta/0]).


-define(PORTA_ESQ,   300).
-define(PORTA_DRE,	301).
-define(MAX_PORTA,	130).
-define(UNACTIVE_COLOR, {246,246,245}).
-define(ACTIVE_COLOR, {255,155,153}).


%% Funcions públiques

new() ->
	case whereis(wxenv) of
		undefined -> wxenv:start();
		_M -> ok
	end,
	register(porta,spawn(?MODULE, porta, [])).

pos_porta(Pos) ->
    porta!{posicio,Pos}.

kill() ->
    porta!kill.

%% -------------------------------

porta() ->
	wx:set_env(wxenv:get()),
    Frame = create_window(),
    L = create_widgets(Frame),
    wxWindow:show(Frame),
    loop(Frame, L),
    ok.

create_window() ->
    Title   = io_lib:format("Porta",[]),
    Frame = wxFrame:new(wx:null(), -1, Title, 
				[{size, {295, 360}},
				{style, ?wxSYSTEM_MENU 
					bor ?wxCAPTION bor ?wxCLOSE_BOX 
					bor ?wxCLIP_CHILDREN}]), % window title
    wxFrame:connect(Frame, close_window),
    Frame.

create_widgets(Frame) ->

	Panel = wxPanel:new(Frame),
    wxPanel:setBackgroundColour(Panel, ?UNACTIVE_COLOR),

    % Create the sizer

    % Create the display and add it to sizer, label : unicode

    DreP = wxPanel:new(Panel, 10, 10, 130, 305),
    wxPanel:setBackgroundColour(DreP, ?ACTIVE_COLOR),
	DreF = wxPanel:new(DreP, 90, 30, 30, 200),
    wxPanel:setBackgroundColour(DreF, ?UNACTIVE_COLOR),
    EsqP = wxPanel:new(Panel, 140,10, 130, 305),
    wxPanel:setBackgroundColour(EsqP, ?ACTIVE_COLOR),
	EsqF = wxPanel:new(EsqP, 10, 30, 30, 200),
    wxPanel:setBackgroundColour(EsqF, ?UNACTIVE_COLOR),
	wxFrame:show(Frame),
	{DreP, EsqP}.


loop(Frame, W) ->
    {DreP, EsqP} = W,
	receive 
   	#wx{event=#wxClose{}} ->
   	    io:format("Closing porta ~n",[]),
	    wxWindow:destroy(Frame),
	    ok;
	kill -> 
	    wxWindow:destroy(Frame),
	    ok;
	{posicio, Pos} when Pos =< ?MAX_PORTA, Pos >= 0 ->
		wxPanel:move(DreP, 10-Pos, 10),
		wxPanel:setBackgroundColour(DreP, ?ACTIVE_COLOR),
		wxPanel:move(EsqP, 140+Pos, 10),
		wxPanel:setBackgroundColour(EsqP, ?ACTIVE_COLOR),
		loop(Frame, W);
	_M -> 
		loop(Frame, W)

    end.