Skip to content

Commit

Permalink
+F: launcher shows commands on buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
diclogic committed Oct 3, 2015
1 parent b225302 commit 4a69490
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
20 changes: 15 additions & 5 deletions src/Launcher/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@

namespace Launcher
{
internal interface IActionListener

public delegate void ActionTriggeredDeleg(int idx);

public interface IControlPanel
{
void OnAction(int idx);
void SetButtonText(int idx, string text);
event ActionTriggeredDeleg ActionTriggered;
}

internal class Controller : IActionListener
internal class Controller
{
IModel m_model;
IRenderer m_renderer;
Dictionary<int, string> m_actions = new Dictionary<int, string>();
IControlPanel m_panel;

public IModel Model { get { return m_model; } }
public IRenderer Renderer { get { return m_renderer; } }

public void Initialize(IRenderer renderer, IModel model, Form mainWindow, UserControl viewportWindow)
public void Initialize(IRenderer renderer, IModel model, IControlPanel panel, Form mainWindow, UserControl viewportWindow)
{
m_renderer = renderer;
m_model = model;
m_panel = panel;

m_model.ActionMapAssigned += OnModelActionMapAssigned;

m_renderer.Model = m_model;

m_panel.ActionTriggered += OnAction;

mainWindow.Move += (_,__) =>
m_model.Input(string.Format("move {0} {1} {2} {3}", mainWindow.Left, mainWindow.Top
, viewportWindow.Width, viewportWindow.Height));
Expand All @@ -51,7 +58,10 @@ protected void OnModelActionMapAssigned(IEnumerable<KeyValuePair<int,string>> ma
{
m_actions = new Dictionary<int, string>();
foreach (var kv in mapping)
{
m_actions[kv.Key] = kv.Value;
m_panel.SetButtonText(kv.Key, kv.Value);
}
}

public void OnAction(int idx)
Expand Down
45 changes: 33 additions & 12 deletions src/Launcher/LauncherWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ namespace Launcher
{


public partial class LauncherWindow : Form
public partial class LauncherWindow : Form, IControlPanel
{
Dictionary<Button, int> m_buttons = new Dictionary<Button, int>();

internal LauncherWindow(IActionListener actionListener)
public event ActionTriggeredDeleg ActionTriggered;

internal LauncherWindow()
{
InitializeComponent();

Expand All @@ -29,21 +32,39 @@ internal LauncherWindow(IActionListener actionListener)

this.glControl1.Paint += new System.Windows.Forms.PaintEventHandler(this.glControl1_Paint);
this.glControl1.Load += new System.EventHandler(this.glControl1_Load);
this.button1.Click += (sender,_) => OnButtonClick((Button)sender, actionListener);
this.button2.Click += (sender,_) => OnButtonClick((Button)sender, actionListener);
this.button3.Click += (sender,_) => OnButtonClick((Button)sender, actionListener);

for (int ii = 0; ii < Controls.Count; ++ii)
{
if (Controls[ii] is Button)
m_buttons.Add(Controls[ii] as Button, 0);
}

var btCtls = m_buttons.Keys.ToArray();

foreach (var b in btCtls)
{
var match = Regex.Match(b.Name, "button([0-9]+)");
if (match.Groups.Count != 2)
return;

var buttonNumber = int.Parse(match.Groups[1].Captures[0].Value);
m_buttons[b] = buttonNumber;

b.Click += (sender,_) => OnButtonClick((Button)sender);
}
}

public GLControl GetGLWindow() { return this.glControl1; }

void OnButtonClick(Button sender, IActionListener listener)
public void SetButtonText(int idx, string text)
{
var match = Regex.Match(((Button)sender).Name, "button([0-9])");
if (match.Groups.Count != 2)
return;
m_buttons.First(kv => kv.Value == idx).Key.Text = text;
}

var buttonNumber = int.Parse(match.Groups[1].Captures[0].Value);
listener.OnAction(buttonNumber);
void OnButtonClick(Button sender)
{
if (ActionTriggered != null)
ActionTriggered(m_buttons[sender]);
}

private void glControl1_Load(object sender, EventArgs e)
Expand All @@ -56,7 +77,7 @@ private void glControl1_Paint(object sender, PaintEventArgs e)
Thread.Sleep(1);
}

}
}


}
7 changes: 4 additions & 3 deletions src/Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ static void Main()
Application.SetCompatibleTextRenderingDefault(false);
try
{
var controller = new Controller();
var mainWindow = new LauncherWindow(controller);
var mainWindow = new LauncherWindow();
var glWindow = mainWindow.GetGLWindow();
var args = Environment.GetCommandLineArgs();

var loader = new ModelLoader();
var model = loader.LoadModel(args.ElementAtOrDefault(1));
var renderer = new Renderer(glWindow);
controller.Initialize(renderer, model, mainWindow, glWindow);

var controller = new Controller();
controller.Initialize(renderer, model, mainWindow, mainWindow, glWindow);

Application.Run(mainWindow);
}
Expand Down

0 comments on commit 4a69490

Please sign in to comment.