(bob) refactor input dispatch; remove public Dispatch*Input from game; (chris) fix build failures due to rebase past gecko

This commit is contained in:
Chris Forbes
2010-11-01 18:39:37 +13:00
parent 527c60daa7
commit d7d0d371c6
13 changed files with 359 additions and 325 deletions

View File

@@ -40,7 +40,7 @@ namespace OpenRA.FileFormats.Graphics
void Begin(); void Begin();
void End(); void End();
void Clear( Color color ); void Clear( Color color );
void Present(); void Present( IInputHandler inputHandler );
void DrawIndexedPrimitives( PrimitiveType type, Range<int> vertexRange, Range<int> indexRange ); void DrawIndexedPrimitives( PrimitiveType type, Range<int> vertexRange, Range<int> indexRange );
void DrawIndexedPrimitives( PrimitiveType type, int vertexPool, int numPrimitives ); void DrawIndexedPrimitives( PrimitiveType type, int vertexPool, int numPrimitives );

View File

@@ -1,4 +1,4 @@
#region Copyright & License Information #region Copyright & License Information
/* /*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS) * Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made * This file is part of OpenRA, which is free software. It is made
@@ -9,16 +9,33 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
namespace OpenRA namespace OpenRA
{ {
public interface IInputHandler
{
void ModifierKeys( Modifiers mods );
void OnKeyInput( KeyInput input );
void OnMouseInput( MouseInput input );
}
public struct MouseInput public struct MouseInput
{ {
public MouseInputEvent Event; public MouseInputEvent Event;
public int2 Location;
public MouseButton Button; public MouseButton Button;
public int2 Location;
public Modifiers Modifiers; public Modifiers Modifiers;
public MouseInput( MouseInputEvent ev, MouseButton button, int2 location, Modifiers mods )
{
this.Event = ev;
this.Button = button;
this.Location = location;
this.Modifiers = mods;
}
} }
public enum MouseInputEvent { Down, Move, Up }; public enum MouseInputEvent { Down, Move, Up };

View File

@@ -45,6 +45,7 @@
</Reference> </Reference>
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="Tao.Sdl, Version=1.2.13.0, Culture=neutral, PublicKeyToken=9c7a200e36c0094e"> <Reference Include="Tao.Sdl, Version=1.2.13.0, Culture=neutral, PublicKeyToken=9c7a200e36c0094e">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
@@ -61,6 +62,7 @@
<Compile Include="FileSystem.cs" /> <Compile Include="FileSystem.cs" />
<Compile Include="Folder.cs" /> <Compile Include="Folder.cs" />
<Compile Include="Graphics\IGraphicsDevice.cs" /> <Compile Include="Graphics\IGraphicsDevice.cs" />
<Compile Include="Graphics\IInputHandler.cs" />
<Compile Include="Graphics\Vertex.cs" /> <Compile Include="Graphics\Vertex.cs" />
<Compile Include="Manifest.cs" /> <Compile Include="Manifest.cs" />
<Compile Include="MiniYaml.cs" /> <Compile Include="MiniYaml.cs" />

View File

@@ -99,7 +99,7 @@ namespace OpenRA
using (new PerfSample("render")) using (new PerfSample("render"))
{ {
++RenderFrame; ++RenderFrame;
viewport.DrawRegions(worldRenderer); viewport.DrawRegions(worldRenderer, new DefaultInputHandler( orderManager.world ));
Sound.SetListenerPosition(viewport.Location + .5f * new float2(viewport.Width, viewport.Height)); Sound.SetListenerPosition(viewport.Location + .5f * new float2(viewport.Width, viewport.Height));
} }
@@ -181,37 +181,14 @@ namespace OpenRA
AfterGameStart( orderManager.world ); AfterGameStart( orderManager.world );
} }
public static void DispatchMouseInput(MouseInputEvent ev, MouseEventArgs e, Modifiers modifierKeys)
{
Sync.CheckSyncUnchanged( orderManager.world, () =>
{
var mi = new MouseInput
{
Button = (MouseButton)(int)e.Button,
Event = ev,
Location = new int2( e.Location ),
Modifiers = modifierKeys,
};
Widget.HandleInput( mi );
} );
}
public static bool IsHost public static bool IsHost
{ {
get { return orderManager.Connection.LocalClientId == 0; } get { return orderManager.Connection.LocalClientId == 0; }
} }
public static void HandleKeyEvent(KeyInput e)
{
Sync.CheckSyncUnchanged( orderManager.world, () =>
{
Widget.HandleKeyPress( e );
} );
}
static Modifiers modifiers; static Modifiers modifiers;
public static Modifiers GetModifierKeys() { return modifiers; } public static Modifiers GetModifierKeys() { return modifiers; }
public static void HandleModifierKeys(Modifiers mods) { modifiers = mods; } internal static void HandleModifierKeys(Modifiers mods) { modifiers = mods; }
internal static void Initialize(Arguments args) internal static void Initialize(Arguments args)
{ {

View File

@@ -92,11 +92,11 @@ namespace OpenRA.Graphics
s.Commit(); s.Commit();
} }
public void EndFrame() public void EndFrame( IInputHandler inputHandler )
{ {
Flush(); Flush();
device.End(); device.End();
device.Present(); device.Present( inputHandler );
} }
public void DrawBatch<T>(IVertexBuffer<T> vertices, IIndexBuffer indices, public void DrawBatch<T>(IVertexBuffer<T> vertices, IIndexBuffer indices,

View File

@@ -96,7 +96,7 @@ namespace OpenRA.Graphics
this.scrollPosition = Game.CellSize* mapStart; this.scrollPosition = Game.CellSize* mapStart;
} }
public void DrawRegions( WorldRenderer wr ) public void DrawRegions( WorldRenderer wr, IInputHandler inputHandler )
{ {
renderer.BeginFrame(scrollPosition); renderer.BeginFrame(scrollPosition);
wr.Draw(); wr.Draw();
@@ -107,7 +107,7 @@ namespace OpenRA.Graphics
var c = new Cursor(cursorName); var c = new Cursor(cursorName);
c.Draw(wr, (int)cursorFrame, Viewport.LastMousePos + Location); c.Draw(wr, (int)cursorFrame, Viewport.LastMousePos + Location);
renderer.EndFrame(); renderer.EndFrame( inputHandler );
} }
public void Tick() public void Tick()

58
OpenRA.Game/InputHandler.cs Executable file
View File

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see LICENSE.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenRA.Widgets;
using OpenRA.Network;
namespace OpenRA
{
public class NullInputHandler : IInputHandler
{
// ignore all input
public void ModifierKeys( Modifiers mods ) { }
public void OnKeyInput( KeyInput input ) { }
public void OnMouseInput( MouseInput input ) { }
}
public class DefaultInputHandler : IInputHandler
{
readonly World world;
public DefaultInputHandler( World world )
{
this.world = world;
}
public void ModifierKeys( Modifiers mods )
{
Game.HandleModifierKeys( mods );
}
public void OnKeyInput( KeyInput input )
{
Sync.CheckSyncUnchanged( world, () =>
{
Widget.HandleKeyPress( input );
} );
}
public void OnMouseInput( MouseInput input )
{
Sync.CheckSyncUnchanged( world, () =>
{
Widget.HandleInput( input );
} );
}
}
}

View File

@@ -139,8 +139,6 @@
<Compile Include="Graphics\SequenceProvider.cs" /> <Compile Include="Graphics\SequenceProvider.cs" />
<Compile Include="Graphics\SheetBuilder.cs" /> <Compile Include="Graphics\SheetBuilder.cs" />
<Compile Include="Graphics\HardwarePalette.cs" /> <Compile Include="Graphics\HardwarePalette.cs" />
<Compile Include="MainWindow.cs">
</Compile>
<Compile Include="Support\Program.cs" /> <Compile Include="Support\Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Graphics\Renderer.cs" /> <Compile Include="Graphics\Renderer.cs" />
@@ -214,6 +212,7 @@
<Compile Include="ActorInitializer.cs" /> <Compile Include="ActorInitializer.cs" />
<Compile Include="ActorReference.cs" /> <Compile Include="ActorReference.cs" />
<Compile Include="Misc.cs" /> <Compile Include="Misc.cs" />
<Compile Include="InputHandler.cs" />
<Compile Include="ModData.cs" /> <Compile Include="ModData.cs" />
<Compile Include="Map.cs" /> <Compile Include="Map.cs" />
<Compile Include="Network\FrameData.cs" /> <Compile Include="Network\FrameData.cs" />

View File

@@ -139,13 +139,13 @@ namespace OpenRA.GlRenderer
CheckGlError(); CheckGlError();
} }
MouseButtons lastButtonBits = (MouseButtons)0; MouseButton lastButtonBits = (MouseButton)0;
static MouseButtons MakeButton(byte b) static MouseButton MakeButton( byte b )
{ {
return b == Sdl.SDL_BUTTON_LEFT ? MouseButtons.Left return b == Sdl.SDL_BUTTON_LEFT ? MouseButton.Left
: b == Sdl.SDL_BUTTON_RIGHT ? MouseButtons.Right : b == Sdl.SDL_BUTTON_RIGHT ? MouseButton.Right
: b == Sdl.SDL_BUTTON_MIDDLE ? MouseButtons.Middle : b == Sdl.SDL_BUTTON_MIDDLE ? MouseButton.Middle
: 0; : 0;
} }
@@ -179,14 +179,14 @@ namespace OpenRA.GlRenderer
} }
} }
public void Present() public void Present( IInputHandler inputHandler )
{ {
Sdl.SDL_GL_SwapBuffers(); Sdl.SDL_GL_SwapBuffers();
Game.HasInputFocus = 0 != ( Sdl.SDL_GetAppState() & Sdl.SDL_APPINPUTFOCUS ); Game.HasInputFocus = 0 != ( Sdl.SDL_GetAppState() & Sdl.SDL_APPINPUTFOCUS );
var mods = MakeModifiers( Sdl.SDL_GetModState() ); var mods = MakeModifiers( Sdl.SDL_GetModState() );
Game.HandleModifierKeys(mods); inputHandler.ModifierKeys( mods );
MouseEventArgs pendingMotion = null; MouseInput? pendingMotion = null;
Sdl.SDL_Event e; Sdl.SDL_Event e;
while( Sdl.SDL_PollEvent( out e ) != 0 ) while( Sdl.SDL_PollEvent( out e ) != 0 )
@@ -201,37 +201,39 @@ namespace OpenRA.GlRenderer
{ {
if( pendingMotion != null ) if( pendingMotion != null )
{ {
Game.DispatchMouseInput(MouseInputEvent.Move, pendingMotion, mods); inputHandler.OnMouseInput( pendingMotion.Value );
pendingMotion = null; pendingMotion = null;
} }
var button = MakeButton( e.button.button ); var button = MakeButton( e.button.button );
lastButtonBits |= button; lastButtonBits |= button;
Game.DispatchMouseInput(MouseInputEvent.Down, inputHandler.OnMouseInput( new MouseInput(
new MouseEventArgs(button, 1, e.button.x, e.button.y, 0), MouseInputEvent.Down, button, new int2( e.button.x, e.button.y ), mods ) );
mods);
} break; } break;
case Sdl.SDL_MOUSEBUTTONUP: case Sdl.SDL_MOUSEBUTTONUP:
{ {
if( pendingMotion != null ) if( pendingMotion != null )
{ {
Game.DispatchMouseInput(MouseInputEvent.Move, pendingMotion, mods); inputHandler.OnMouseInput( pendingMotion.Value );
pendingMotion = null; pendingMotion = null;
} }
var button = MakeButton( e.button.button ); var button = MakeButton( e.button.button );
lastButtonBits &= ~button; lastButtonBits &= ~button;
Game.DispatchMouseInput(MouseInputEvent.Up, inputHandler.OnMouseInput( new MouseInput(
new MouseEventArgs(button, 1, e.button.x, e.button.y, 0), MouseInputEvent.Up, button, new int2( e.button.x, e.button.y ), mods ) );
mods);
} break; } break;
case Sdl.SDL_MOUSEMOTION: case Sdl.SDL_MOUSEMOTION:
{ {
pendingMotion = new MouseEventArgs(lastButtonBits, 0, e.motion.x, e.motion.y, 0); pendingMotion = new MouseInput(
MouseInputEvent.Move,
lastButtonBits,
new int2( e.motion.x, e.motion.y ),
mods );
} break; } break;
case Sdl.SDL_KEYDOWN: case Sdl.SDL_KEYDOWN:
@@ -246,7 +248,7 @@ namespace OpenRA.GlRenderer
}; };
if( !HandleSpecialKey( keyEvent ) ) if( !HandleSpecialKey( keyEvent ) )
Game.HandleKeyEvent(keyEvent); inputHandler.OnKeyInput( keyEvent );
} break; } break;
case Sdl.SDL_KEYUP: case Sdl.SDL_KEYUP:
@@ -260,14 +262,14 @@ namespace OpenRA.GlRenderer
VirtKey = e.key.keysym.sym VirtKey = e.key.keysym.sym
}; };
Game.HandleKeyEvent(keyEvent); inputHandler.OnKeyInput( keyEvent );
} break; } break;
} }
} }
if( pendingMotion != null ) if( pendingMotion != null )
{ {
Game.DispatchMouseInput(MouseInputEvent.Move, pendingMotion, mods); inputHandler.OnMouseInput( pendingMotion.Value );
pendingMotion = null; pendingMotion = null;
} }

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Mods.Cnc
WidgetUtils.FillRectWithSprite(StripeRect, Stripe); WidgetUtils.FillRectWithSprite(StripeRect, Stripe);
r.RgbaSpriteRenderer.DrawSprite(Logo, LogoPos); r.RgbaSpriteRenderer.DrawSprite(Logo, LogoPos);
Font.DrawText(text, new float2(Renderer.Resolution.Width - textSize.X - 20, Renderer.Resolution.Height - textSize.Y - 20), Color.White); Font.DrawText(text, new float2(Renderer.Resolution.Width - textSize.X - 20, Renderer.Resolution.Height - textSize.Y - 20), Color.White);
r.EndFrame(); r.EndFrame( new NullInputHandler() );
} }
} }
} }

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.RA
// Draw a black screen // Draw a black screen
Game.Renderer.BeginFrame(float2.Zero); Game.Renderer.BeginFrame(float2.Zero);
Game.Renderer.EndFrame(); Game.Renderer.EndFrame( new NullInputHandler() );
} }
} }
} }

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Mods.RA
WidgetUtils.FillRectWithSprite(StripeRect, Stripe); WidgetUtils.FillRectWithSprite(StripeRect, Stripe);
r.RgbaSpriteRenderer.DrawSprite(Logo, LogoPos); r.RgbaSpriteRenderer.DrawSprite(Logo, LogoPos);
Font.DrawText(text, new float2(Renderer.Resolution.Width - textSize.X - 20, Renderer.Resolution.Height - textSize.Y - 20), Color.White); Font.DrawText(text, new float2(Renderer.Resolution.Width - textSize.X - 20, Renderer.Resolution.Height - textSize.Y - 20), Color.White);
r.EndFrame(); r.EndFrame( new NullInputHandler() );
} }
} }
} }

View File

@@ -1,10 +1,8 @@
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Windows.Forms;
using OpenRA.FileFormats.Graphics; using OpenRA.FileFormats.Graphics;
using OpenRA.Graphics; using OpenRA.Graphics;
[assembly: Renderer(typeof(OpenRA.Renderer.Null.NullGraphicsDevice))] [assembly: Renderer(typeof(OpenRA.Renderer.Null.NullGraphicsDevice))]
namespace OpenRA.Renderer.Null namespace OpenRA.Renderer.Null
@@ -18,42 +16,23 @@ namespace OpenRA.Renderer.Null
WindowSize = new Size(width, height); WindowSize = new Size(width, height);
} }
public void EnableScissor(int left, int top, int width, int height) public void EnableScissor(int left, int top, int width, int height) { }
{ public void DisableScissor() { }
}
public void DisableScissor()
{
}
public void Begin() { } public void Begin() { }
public void End() { } public void End() { }
public void Clear(Color c) { }
public void Clear(Color c) public void Present(IInputHandler ih)
{
}
public void Present()
{ {
Game.HasInputFocus = false; Game.HasInputFocus = false;
Game.HandleModifierKeys(Modifiers.None); ih.ModifierKeys(Modifiers.None);
} }
public void DrawIndexedPrimitives(PrimitiveType pt, Range<int> vertices, Range<int> indices) public void DrawIndexedPrimitives(PrimitiveType pt, Range<int> vertices, Range<int> indices) { }
{ public void DrawIndexedPrimitives(PrimitiveType pt, int numVerts, int numPrimitives) { }
}
public void DrawIndexedPrimitives(PrimitiveType pt, int numVerts, int numPrimitives)
{
}
public IVertexBuffer<Vertex> CreateVertexBuffer(int size)
{
return new NullVertexBuffer<Vertex>();
}
public IVertexBuffer<Vertex> CreateVertexBuffer(int size) { return new NullVertexBuffer<Vertex>(); }
public IIndexBuffer CreateIndexBuffer(int size) { return new NullIndexBuffer(); } public IIndexBuffer CreateIndexBuffer(int size) { return new NullIndexBuffer(); }
public ITexture CreateTexture() { return new NullTexture(); } public ITexture CreateTexture() { return new NullTexture(); }
public ITexture CreateTexture(Bitmap bitmap) { return new NullTexture(); } public ITexture CreateTexture(Bitmap bitmap) { return new NullTexture(); }