[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] r3050 - trunk/pingus/src/input2
From: |
grumbel at BerliOS |
Subject: |
[Pingus-CVS] r3050 - trunk/pingus/src/input2 |
Date: |
Sat, 1 Sep 2007 21:48:18 +0200 |
Author: grumbel
Date: 2007-09-01 21:48:17 +0200 (Sat, 01 Sep 2007)
New Revision: 3050
Added:
trunk/pingus/src/input2/controller.cpp
trunk/pingus/src/input2/core_driver.cpp
Modified:
trunk/pingus/src/input2/SConstruct
trunk/pingus/src/input2/control.hpp
trunk/pingus/src/input2/controller.hpp
trunk/pingus/src/input2/core_driver.hpp
trunk/pingus/src/input2/manager.cpp
trunk/pingus/src/input2/manager.hpp
Log:
- implemented AxisPointer
- some further cleanup
Modified: trunk/pingus/src/input2/SConstruct
===================================================================
--- trunk/pingus/src/input2/SConstruct 2007-09-01 17:43:59 UTC (rev 3049)
+++ trunk/pingus/src/input2/SConstruct 2007-09-01 19:48:17 UTC (rev 3050)
@@ -9,6 +9,8 @@
env.Program('main', [
'main.cpp',
'controller_description.cpp',
+ 'controller.cpp',
+ 'core_driver.cpp',
'sdl_driver.cpp',
'manager.cpp',
'../file_reader.cpp',
Modified: trunk/pingus/src/input2/control.hpp
===================================================================
--- trunk/pingus/src/input2/control.hpp 2007-09-01 17:43:59 UTC (rev 3049)
+++ trunk/pingus/src/input2/control.hpp 2007-09-01 19:48:17 UTC (rev 3050)
@@ -28,6 +28,7 @@
#include "math.hpp"
#include "math/vector2f.hpp"
#include "event.hpp"
+#include "controller.hpp"
namespace Input {
@@ -55,6 +56,9 @@
}
}
+ virtual void update(float delta) {
+ }
+
virtual void update(Control* ctrl) {
std::cout << "Warning: Control:update() not handled" << std::endl;
}
@@ -97,6 +101,11 @@
buttons.push_back(button);
}
+ void update(float delta) {
+ for(std::vector<Button*>::iterator i = buttons.begin(); i !=
buttons.end(); ++i)
+ (*i)->update(delta);
+ }
+
virtual void update(Control* ctrl)
{
ButtonState new_state = BUTTON_RELEASED;
@@ -119,17 +128,18 @@
class ControllerButton : public ButtonGroup
{
private:
+ Controller* controller;
int id;
public:
- ControllerButton(int id_)
+ ControllerButton(Controller* controller_, int id_)
: ButtonGroup(0),
+ controller(controller_),
id(id_)
{}
virtual void notify_parent() {
- std::cout << "ControlButton: id=" << id << " was " << (state ==
BUTTON_PRESSED
- ? "pressed" : "released") <<
std::endl;
+ controller->add_button_event(id, state);
}
};
@@ -211,6 +221,11 @@
axes.push_back(axis);
}
+ void update(float delta) {
+ for(std::vector<Axis*>::iterator i = axes.begin(); i != axes.end(); ++i)
+ (*i)->update(delta);
+ }
+
void update(Control* ctrl)
{
float new_pos = 0;
@@ -233,16 +248,18 @@
class ControllerAxis : public AxisGroup
{
private:
+ Controller* controller;
int id;
public:
- ControllerAxis(int id_)
+ ControllerAxis(Controller* controller_, int id_)
: AxisGroup(0),
+ controller(controller_),
id(id_)
{}
virtual void notify_parent() {
- std::cout << "ControlAxis moved id=" << id << " " << pos << std::endl;
+ controller->add_axis_event(id, pos);
}
};
@@ -267,6 +284,11 @@
}
}
+ void update(float delta) {
+ for(std::vector<Pointer*>::iterator i = pointer.begin(); i !=
pointer.end(); ++i)
+ (*i)->update(delta);
+ }
+
void add_pointer(Pointer* p) {
pointer.push_back(p);
}
@@ -275,16 +297,18 @@
class ControllerPointer : public PointerGroup
{
private:
+ Controller* controller;
int id;
public:
- ControllerPointer(int id_)
+ ControllerPointer(Controller* controller_, int id_)
: PointerGroup(0),
+ controller(controller_),
id(id_)
{}
virtual void notify_parent() {
- std::cout << "ControlPointer moved id=" << id << " " << pos.x << ", " <<
pos.y << std::endl;
+ controller->add_pointer_event(id, pos.x, pos.y);
}
};
@@ -298,6 +322,11 @@
: Scroller(parent)
{}
+ void update(float delta) {
+ for(std::vector<Scroller*>::iterator i = scrollers.begin(); i !=
scrollers.end(); ++i)
+ (*i)->update(delta);
+ }
+
void update(Control* p) {
Scroller* scroller = dynamic_cast<Scroller*>(p);
assert(scroller);
@@ -313,15 +342,18 @@
class ControllerScroller : public ScrollerGroup
{
private:
+ Controller* controller;
int id;
+
public:
- ControllerScroller(int id_)
+ ControllerScroller(Controller* controller_, int id_)
: ScrollerGroup(0),
+ controller(controller_),
id(id_)
{}
virtual void notify_parent() {
- std::cout << "ControlScroller: moved id=" << id << " " << delta.x << ", "
<< delta.y << std::endl;
+ controller->add_scroller_event(id, delta.x, delta.y);
}
};
Added: trunk/pingus/src/input2/controller.cpp
===================================================================
--- trunk/pingus/src/input2/controller.cpp 2007-09-01 17:43:59 UTC (rev
3049)
+++ trunk/pingus/src/input2/controller.cpp 2007-09-01 19:48:17 UTC (rev
3050)
@@ -0,0 +1,179 @@
+/* $Id$
+** __ __ __ ___ __ __ __ __
+** / \ / \__| ____ __| _/_______/ |_|__| | | | ____
+** \ \/\/ / |/ \ / __ |/ ___/\ __\ | | | | _/ __ \
+** \ /| | | \/ /_/ |\___ \ | | | | |_| |_\ ___/
+** \__/\ / |__|___| /\____ /____ > |__| |__|____/____/\___ >
+** \/ \/ \/ \/ \/
+** Copyright (C) 2007 Ingo Ruhnke <address@hidden>
+**
+** This program is free software; you can redistribute it and/or
+** modify it under the terms of the GNU General Public License
+** as published by the Free Software Foundation; either version 2
+** of the License, or (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+** 02111-1307, USA.
+*/
+
+#include "controller_description.hpp"
+#include "control.hpp"
+#include "controller.hpp"
+
+namespace Input {
+
+void
+Controller::load(const ControllerDescription& desc)
+{
+ const std::vector<int>& button_lst = desc.get_buttons();
+ for(std::vector<int>::const_iterator i = button_lst.begin(); i !=
button_lst.end(); ++i)
+ {
+ add_button(*i, new ControllerButton(this, *i));
+ }
+
+ const std::vector<int>& axis_lst = desc.get_axes();
+ for(std::vector<int>::const_iterator i = axis_lst.begin(); i !=
axis_lst.end(); ++i)
+ {
+ add_axis(*i, new ControllerAxis(this, *i));
+ }
+
+ const std::vector<int>& pointer_lst = desc.get_pointers();
+ for(std::vector<int>::const_iterator i = pointer_lst.begin(); i !=
pointer_lst.end(); ++i)
+ {
+ add_pointer(*i, new ControllerPointer(this, *i));
+ }
+
+ const std::vector<int>& scroller_lst = desc.get_scrollers();
+ for(std::vector<int>::const_iterator i = scroller_lst.begin(); i !=
scroller_lst.end(); ++i)
+ {
+ add_scroller(*i, new ControllerScroller(this, *i));
+ }
+}
+
+
+ControllerScroller*
+Controller::get_scroller(int id)
+{
+ if (id >= 0 && id < int(scrollers.size()))
+ return scrollers[id];
+ else
+ return 0;
+}
+
+void
+Controller::add_scroller(int id, ControllerScroller* scroller)
+{
+ if (int(scrollers.size())-1 < id)
+ scrollers.resize(id+1);
+
+ assert(scrollers[id] == 0);
+ scrollers[id] = scroller;
+}
+
+ControllerPointer*
+Controller::get_pointer(int id)
+{
+ if (id >= 0 && id < int(pointers.size()))
+ return pointers[id];
+ else
+ return 0;
+}
+
+void
+Controller::add_pointer(int id, ControllerPointer* pointer)
+{
+ if (int(pointers.size())-1 < id)
+ pointers.resize(id+1);
+
+ assert(pointers[id] == 0);
+ pointers[id] = pointer;
+}
+
+ControllerAxis*
+Controller::get_axis(int id)
+{
+ assert(id >= 0 && id < int(axes.size()));
+ return axes[id];
+}
+
+void
+Controller::add_axis(int id, ControllerAxis* axis)
+{
+ if (int(axes.size())-1 < id)
+ axes.resize(id+1);
+
+ assert(axes[id] == 0);
+ axes[id] = axis;
+}
+
+ControllerButton*
+Controller::get_button(int id)
+{
+ assert(id >= 0 && id < int(buttons.size()));
+ return buttons[id];
+}
+
+void
+Controller::update(float delta)
+{
+ for(std::vector<ControllerButton*>::iterator i = buttons.begin();
+ i != buttons.end(); ++i)
+ if (*i) (*i)->update(delta);
+
+ for(std::vector<ControllerAxis*>::iterator i = axes.begin();
+ i != axes.end(); ++i)
+ if (*i) (*i)->update(delta);
+
+ for(std::vector<ControllerPointer*>::iterator i = pointers.begin();
+ i != pointers.end(); ++i)
+ if (*i) (*i)->update(delta);
+
+ for(std::vector<ControllerScroller*>::iterator i = scrollers.begin();
+ i != scrollers.end(); ++i)
+ if (*i) (*i)->update(delta);
+}
+
+void
+Controller::add_button(int id, ControllerButton* button)
+{
+ if (int(buttons.size())-1 < id)
+ buttons.resize(id+1);
+
+ assert(buttons[id] == 0);
+ buttons[id] = button;
+}
+
+void
+Controller::add_axis_event(int id, float pos)
+{
+ std::cout << "Controller::axis_event: id=" << id << " " << pos << std::endl;
+}
+
+void
+Controller::add_button_event(int id, ButtonState state)
+{
+ std::cout << "Controller::button_event: id=" << id << " " << state <<
std::endl;
+}
+
+void
+Controller::add_pointer_event(int id, float x, float y)
+{
+ std::cout << "Controller::pointer_event: id=" << id << " " << x << ", " << y
<< std::endl;
+}
+
+void
+Controller::add_scroller_event(int id, float xrel, float yrel)
+{
+ std::cout << "Controller::scroller_event: id=" << id << " " << xrel << ", "
<< yrel << std::endl;
+}
+
+} // namespace Input
+
+/* EOF */
Property changes on: trunk/pingus/src/input2/controller.cpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: trunk/pingus/src/input2/controller.hpp
===================================================================
--- trunk/pingus/src/input2/controller.hpp 2007-09-01 17:43:59 UTC (rev
3049)
+++ trunk/pingus/src/input2/controller.hpp 2007-09-01 19:48:17 UTC (rev
3050)
@@ -21,12 +21,17 @@
#define HEADER_PINGUS_INPUT_CONTROLLER_HXX
#include <assert.h>
-#include "control.hpp"
-#include "controller_description.hpp"
#include "event.hpp"
namespace Input {
+class ControllerDescription;
+
+class ControllerButton;
+class ControllerAxis;
+class ControllerPointer;
+class ControllerScroller;
+
class Controller
{
private:
@@ -43,89 +48,24 @@
Controller() {}
~Controller() {}
- ControllerScroller* get_scroller(int id) {
- if (id >= 0 && id < int(scrollers.size()))
- return scrollers[id];
- else
- return 0;
- }
+ ControllerAxis* get_axis(int id);
+ ControllerButton* get_button(int id);
+ ControllerPointer* get_pointer(int id);
+ ControllerScroller* get_scroller(int id);
- void add_scroller(int id, ControllerScroller* scroller) {
- if (int(scrollers.size())-1 < id)
- scrollers.resize(id+1);
-
- assert(scrollers[id] == 0);
- scrollers[id] = scroller;
- }
-
- ControllerPointer* get_pointer(int id) {
- if (id >= 0 && id < int(pointers.size()))
- return pointers[id];
- else
- return 0;
- }
+ void add_axis_event(int id, float pos);
+ void add_button_event(int id, ButtonState state);
+ void add_pointer_event(int id, float x, float y);
+ void add_scroller_event(int id, float xrel, float yrel);
- void add_pointer(int id, ControllerPointer* pointer) {
- if (int(pointers.size())-1 < id)
- pointers.resize(id+1);
-
- assert(pointers[id] == 0);
- pointers[id] = pointer;
- }
+ void add_axis(int id, ControllerAxis* axis);
+ void add_button(int id, ControllerButton* button);
+ void add_pointer(int id, ControllerPointer* pointer);
+ void add_scroller(int id, ControllerScroller* scroller);
- ControllerAxis* get_axis(int id) {
- assert(id >= 0 && id < int(axes.size()));
- return axes[id];
- }
+ void load(const ControllerDescription& desc);
- void add_axis(int id, ControllerAxis* axis) {
- if (int(axes.size())-1 < id)
- axes.resize(id+1);
-
- assert(axes[id] == 0);
- axes[id] = axis;
- }
-
- ControllerButton* get_button(int id) {
- assert(id >= 0 && id < int(buttons.size()));
- return buttons[id];
- }
-
- void add_button(int id, ControllerButton* button) {
- if (int(buttons.size())-1 < id)
- buttons.resize(id+1);
-
- assert(buttons[id] == 0);
- buttons[id] = button;
- }
-
- void load(ControllerDescription desc)
- {
- const std::vector<int>& button_lst = desc.get_buttons();
- for(std::vector<int>::const_iterator i = button_lst.begin(); i !=
button_lst.end(); ++i)
- {
- add_button(*i, new ControllerButton(*i));
- }
-
- const std::vector<int>& axis_lst = desc.get_axes();
- for(std::vector<int>::const_iterator i = axis_lst.begin(); i !=
axis_lst.end(); ++i)
- {
- add_axis(*i, new ControllerAxis(*i));
- }
-
- const std::vector<int>& pointer_lst = desc.get_pointers();
- for(std::vector<int>::const_iterator i = pointer_lst.begin(); i !=
pointer_lst.end(); ++i)
- {
- add_pointer(*i, new ControllerPointer(*i));
- }
-
- const std::vector<int>& scroller_lst = desc.get_scrollers();
- for(std::vector<int>::const_iterator i = scroller_lst.begin(); i !=
scroller_lst.end(); ++i)
- {
- add_scroller(*i, new ControllerScroller(*i));
- }
- }
-
+ void update(float delta);
private:
Controller(const Controller&);
Controller& operator= (const Controller&);
Added: trunk/pingus/src/input2/core_driver.cpp
===================================================================
--- trunk/pingus/src/input2/core_driver.cpp 2007-09-01 17:43:59 UTC (rev
3049)
+++ trunk/pingus/src/input2/core_driver.cpp 2007-09-01 19:48:17 UTC (rev
3050)
@@ -0,0 +1,144 @@
+/* $Id$
+** __ __ __ ___ __ __ __ __
+** / \ / \__| ____ __| _/_______/ |_|__| | | | ____
+** \ \/\/ / |/ \ / __ |/ ___/\ __\ | | | | _/ __ \
+** \ /| | | \/ /_/ |\___ \ | | | | |_| |_\ ___/
+** \__/\ / |__|___| /\____ /____ > |__| |__|____/____/\___ >
+** \/ \/ \/ \/ \/
+** Copyright (C) 2007 Ingo Ruhnke <address@hidden>
+**
+** This program is free software; you can redistribute it and/or
+** modify it under the terms of the GNU General Public License
+** as published by the Free Software Foundation; either version 2
+** of the License, or (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+** 02111-1307, USA.
+*/
+
+#include "math.hpp"
+#include "manager.hpp"
+#include "core_driver.hpp"
+
+namespace Input {
+
+class AxisPointer : public Pointer
+{
+private:
+ Axis* x_axis;
+ Axis* y_axis;
+ float speed;
+
+public:
+ AxisPointer(Control* parent)
+ : Pointer(parent),
+ x_axis(0), y_axis(0),
+ speed(800.0f)
+ {
+ }
+
+ ~AxisPointer()
+ {
+ }
+
+ void set_axis(Axis* x, Axis* y)
+ {
+ x_axis = x;
+ y_axis = y;
+ }
+
+ void update(Control* )
+ {
+ //std::cout << "event" << std::endl;
+ }
+
+ void update(float delta)
+ {
+ Vector2f new_pos = pos;
+
+ new_pos.x += x_axis->get_pos() * speed * delta;
+ new_pos.y += y_axis->get_pos() * speed * delta;
+
+ // FIXME: Shouldn't be hardcored, but shouldn't depend on Display
+ // either
+ new_pos.x = Math::clamp(0.0f, new_pos.x, 800.0f);
+ new_pos.y = Math::clamp(0.0f, new_pos.y, 600.0f);
+
+ if (new_pos != pos)
+ {
+ pos = new_pos;
+ notify_parent();
+ }
+ }
+};
+
+Button*
+CoreDriver::create_button(const FileReader& reader, Control* parent)
+{
+ return 0;
+}
+
+Axis*
+CoreDriver::create_axis(const FileReader& reader, Control* parent)
+{
+ return 0;
+}
+
+Scroller*
+CoreDriver::create_scroller(const FileReader& reader, Control* parent)
+{
+ return 0;
+}
+
+Pointer*
+CoreDriver::create_pointer(const FileReader& reader, Control* parent)
+{
+ if (reader.get_name() == "core:axis-pointer")
+ {
+ AxisPointer* axis = new AxisPointer(parent);
+
+ FileReader x_reader;
+ if (!reader.read_section("x-axis", x_reader))
+ {
+ std::cout << "CoreDriver: Couldn't find x-axis" << std::endl;
+ delete axis;
+ return 0;
+ }
+
+ FileReader y_reader;
+ if (!reader.read_section("y-axis", y_reader))
+ {
+ std::cout << "CoreDriver: Couldn't find y-axis" << std::endl;
+ delete axis;
+ return 0;
+ }
+
+ Axis* x_axis = manager->create_axis(x_reader.get_sections().front(),
axis);
+ Axis* y_axis = manager->create_axis(y_reader.get_sections().front(),
axis);
+
+ if (x_axis && y_axis)
+ {
+ axis->set_axis(x_axis, y_axis);
+ return axis;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+} // namespace Input
+
+/* EOF */
Property changes on: trunk/pingus/src/input2/core_driver.cpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: trunk/pingus/src/input2/core_driver.hpp
===================================================================
--- trunk/pingus/src/input2/core_driver.hpp 2007-09-01 17:43:59 UTC (rev
3049)
+++ trunk/pingus/src/input2/core_driver.hpp 2007-09-01 19:48:17 UTC (rev
3050)
@@ -26,24 +26,29 @@
#ifndef HEADER_CORE_DRIVER_HPP
#define HEADER_CORE_DRIVER_HPP
+#include "driver.hpp"
+
namespace Input {
+class Manager;
+
/** */
class CoreDriver : public Driver
{
private:
+ Manager* manager;
public:
- CoreDriver() {}
+ CoreDriver(Manager* manager_) : manager(manager_) {}
virtual ~CoreDriver() {}
std::string get_name() { return "core"; }
void update(float delta) {}
- Button* create_button (const FileReader& reader, Control* parent) {
return 0; }
- Axis* create_axis (const FileReader& reader, Control* parent) {
return 0; }
- Scroller* create_scroller(const FileReader& reader, Control* parent) {
return 0; }
- Pointer* create_pointer (const FileReader& reader, Control* parent) {
return 0; }
+ Button* create_button (const FileReader& reader, Control* parent);
+ Axis* create_axis (const FileReader& reader, Control* parent);
+ Scroller* create_scroller(const FileReader& reader, Control* parent);
+ Pointer* create_pointer (const FileReader& reader, Control* parent);
};
} // namespace Input
Modified: trunk/pingus/src/input2/manager.cpp
===================================================================
--- trunk/pingus/src/input2/manager.cpp 2007-09-01 17:43:59 UTC (rev 3049)
+++ trunk/pingus/src/input2/manager.cpp 2007-09-01 19:48:17 UTC (rev 3050)
@@ -169,9 +169,9 @@
Manager::update(float delta)
{
for(Drivers::iterator i = drivers.begin(); i != drivers.end(); ++i)
- {
- (*i)->update(delta);
- }
+ (*i)->update(delta);
+
+ controller.update(delta);
}
Driver*
@@ -203,7 +203,7 @@
if (name == "sdl") {
driver = new SDLDriver();
} else if (name == "core") {
- driver = new CoreDriver();
+ driver = new CoreDriver(this);
} else {
std::cout << "Manager: Unknown driver: " << name << std::endl;
return 0;
@@ -215,7 +215,7 @@
}
Button*
-Manager::create_button(const FileReader& reader, Button* parent)
+Manager::create_button(const FileReader& reader, Control* parent)
{
std::string driver = get_driver_part(reader.get_name());
@@ -232,7 +232,7 @@
}
Axis*
-Manager::create_axis(const FileReader& reader, Axis* parent)
+Manager::create_axis(const FileReader& reader, Control* parent)
{
std::string driver = get_driver_part(reader.get_name());
@@ -249,7 +249,7 @@
}
Pointer*
-Manager::create_pointer(const FileReader& reader, Pointer* parent)
+Manager::create_pointer(const FileReader& reader, Control* parent)
{
std::string driver = get_driver_part(reader.get_name());
@@ -266,7 +266,7 @@
}
Scroller*
-Manager::create_scroller(const FileReader& reader, Scroller* parent)
+Manager::create_scroller(const FileReader& reader, Control* parent)
{
std::string driver = get_driver_part(reader.get_name());
Modified: trunk/pingus/src/input2/manager.hpp
===================================================================
--- trunk/pingus/src/input2/manager.hpp 2007-09-01 17:43:59 UTC (rev 3049)
+++ trunk/pingus/src/input2/manager.hpp 2007-09-01 19:48:17 UTC (rev 3050)
@@ -44,11 +44,10 @@
Driver* load_driver(const std::string& name);
Driver* get_driver(const std::string& name);
-private:
- Button* create_button (const FileReader& reader, Button* parent);
- Axis* create_axis (const FileReader& reader, Axis* parent);
- Pointer* create_pointer (const FileReader& reader, Pointer* parent);
- Scroller* create_scroller(const FileReader& reader, Scroller* parent);
+ Button* create_button (const FileReader& reader, Control* parent);
+ Axis* create_axis (const FileReader& reader, Control* parent);
+ Pointer* create_pointer (const FileReader& reader, Control* parent);
+ Scroller* create_scroller(const FileReader& reader, Control* parent);
};
} // namespace Input
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] r3050 - trunk/pingus/src/input2,
grumbel at BerliOS <=