ZVISION: Have PushToggleControl inherit from MouseEvent and handle the methods internally.

Rather than creating an instance of MouseEvent and passing argument around.
This commit is contained in:
richiesams 2013-08-24 00:06:20 -05:00
parent 7095e613d5
commit 3054489a8b
3 changed files with 65 additions and 17 deletions

View File

@ -108,10 +108,9 @@ void Control::parseTiltControl(ZVision *engine, Common::SeekableReadStream &stre
// PushToggleControl
//////////////////////////////////////////////////////////////////////////////
PushToggleControl::PushToggleControl(uint32 key, Common::SeekableReadStream &stream)
: Control() {
_event._key = _key = key;
PushToggleControl::PushToggleControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream)
: MouseEvent(key),
_engine(engine) {
// Loop until we find the closing brace
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
@ -125,27 +124,27 @@ PushToggleControl::PushToggleControl(uint32 key, Common::SeekableReadStream &str
sscanf(line.c_str(), "%*[^(](%u,%u,%u,%u)", &x, &y, &width, &height);
_event._hotspot = Common::Rect(x, y, x + width, y + height);
_hotspot = Common::Rect(x, y, x + width, y + height);
} else if (line.matchString("cursor*", true)) {
char nameBuffer[25];
sscanf(line.c_str(), "%*[^(](%25[^)])", nameBuffer);
_event._hoverCursor = Common::String(nameBuffer);
_hoverCursor = Common::String(nameBuffer);
}
line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
}
if (_event._hotspot.isEmpty() || _event._hoverCursor.empty()) {
if (_hotspot.isEmpty() || _hoverCursor.empty()) {
warning("Push_toggle cursor %u was parsed incorrectly", key);
}
}
bool PushToggleControl::enable(ZVision *engine) {
bool PushToggleControl::enable() {
if (!_enabled) {
engine->registerMouseEvent(_event);
_engine->registerMouseEvent(this);
_enabled = true;
return true;
}
@ -154,9 +153,9 @@ bool PushToggleControl::enable(ZVision *engine) {
return false;
}
bool PushToggleControl::disable(ZVision *engine) {
bool PushToggleControl::disable() {
if (_enabled) {
engine->removeMouseEvent(_key);
_engine->removeMouseEvent(_key);
_enabled = false;
return true;
}
@ -165,4 +164,16 @@ bool PushToggleControl::disable(ZVision *engine) {
return false;
}
void PushToggleControl::onMouseDown(const Common::Point &screenSpacePos, const Common::Point backgroundImageSpacePos) {
_engine->getScriptManager()->setStateValue(_key, 1);
}
bool PushToggleControl::onMouseMove(const Common::Point &screenSpacePos, const Common::Point backgroundImageSpacePos) {
if (_hotspot.contains(backgroundImageSpacePos)) {
_engine->getCursorManager()->changeCursor(_hoverCursor);
return true;
}
return false;
}
} // End of namespace ZVision

View File

@ -52,14 +52,51 @@ public:
static void parseTiltControl(ZVision *engine, Common::SeekableReadStream &stream);
};
class PushToggleControl : public Control {
class PushToggleControl : public Control, public MouseEvent {
public:
PushToggleControl(ZVision *engine, uint32 key, Common::SeekableReadStream &stream);
bool enable();
bool disable();
/**
* Called when LeftMouse is pushed. Calls ScriptManager::setStateValue(_key, 1);
*
* @param screenSpacePos The position of the mouse in screen space
* @param backgroundImageSpacePos The position of the mouse in background image space
*/
void onMouseDown(const Common::Point &screenSpacePos, const Common::Point backgroundImageSpacePos);
/**
* Called when LeftMouse is lifted. Does nothing
*
* @param screenSpacePos The position of the mouse in screen space
* @param backgroundImageSpacePos The position of the mouse in background image space
*/
void onMouseUp(const Common::Point &screenSpacePos, const Common::Point backgroundImageSpacePos) {}
/**
* Called on every MouseMove. Tests if the mouse is inside _hotspot, and if so, sets the cursor.
*
* @param engine The base engine
* @param screenSpacePos The position of the mouse in screen space
* @param backgroundImageSpacePos The position of the mouse in background image space
* @return Was the cursor changed?
*/
bool onMouseMove(const Common::Point &screenSpacePos, const Common::Point backgroundImageSpacePos);
private:
ZVision * _engine;
/**
* The area that will trigger the event
* This is in image space coordinates, NOT screen space
*/
Common::Rect _hotspot;
/** The cursor to use when hovering over _hotspot */
Common::String _hoverCursor;
};
public:
PushToggleControl(uint32 key, Common::SeekableReadStream &stream);
bool enable(ZVision *engine);
bool disable(ZVision *engine);
private:
MouseEvent _event;
};
// TODO: Implement InputControl

View File

@ -278,7 +278,7 @@ void ScriptManager::parseControl(Common::String &line, Common::SeekableReadStrea
Common::String controlType(controlTypeBuffer);
if (controlType.equalsIgnoreCase("push_toggle")) {
_activeControls[key] = new PushToggleControl(key, stream);
_activeControls[key] = new PushToggleControl(_engine, key, stream);
return;
} else if (controlType.equalsIgnoreCase("flat")) {
Control::parseFlatControl(_engine);