Skip to content

Commit

Permalink
ZVISION: Implement full code for input control
Browse files Browse the repository at this point in the history
  • Loading branch information
Marisa-Chan committed Aug 4, 2014
1 parent 73d26bc commit 550c4db
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 25 deletions.
149 changes: 126 additions & 23 deletions engines/zvision/scripting/controls/input_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "common/scummsys.h"

#include "zvision/scripting/controls/input_control.h"
#include "zvision/cursors/cursor_manager.h"

#include "zvision/zvision.h"
#include "zvision/scripting/script_manager.h"
Expand All @@ -42,7 +43,11 @@ InputControl::InputControl(ZVision *engine, uint32 key, Common::SeekableReadStre
_nextTabstop(0),
_focused(false),
_textChanged(false),
_cursorOffset(0) {
_cursorOffset(0),
_enterPressed(false),
_readOnly(false),
_txtWidth(0),
_animation(NULL) {
// Loop until we find the closing brace
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
Expand Down Expand Up @@ -71,21 +76,30 @@ InputControl::InputControl(ZVision *engine, uint32 key, Common::SeekableReadStre

sscanf(line.c_str(), "%*[^(](%u)", &fontFormatNumber);

_textStyle = _engine->getStringManager()->getTextStyle(fontFormatNumber);
_string_init.readAllStyle(_engine->getStringManager()->getTextLine(fontFormatNumber));
} else if (line.matchString("*chooser_init_string*", true)) {
uint fontFormatNumber;

sscanf(line.c_str(), "%*[^(](%u)", &fontFormatNumber);

_string_chooser_init.readAllStyle(_engine->getStringManager()->getTextLine(fontFormatNumber));
} else if (line.matchString("*next_tabstop*", true)) {
sscanf(line.c_str(), "%*[^(](%u)", &_nextTabstop);
} else if (line.matchString("*cursor_dimensions*", true)) {
// Ignore, use the dimensions in the animation file
} else if (line.matchString("*cursor_animation_frames*", true)) {
// Ignore, use the frame count in the animation file
} else if (line.matchString("*cursor_animation*", true)) {
char fileName[25];

sscanf(line.c_str(), "%*[^(](%25s %*u)", fileName);

_cursorAnimationFileName = Common::String(fileName);
} else if (line.matchString("*cursor_dimensions*", true)) {
// Ignore, use the dimensions in the animation file
} else if (line.matchString("*cursor_animation_frames*", true)) {
// Ignore, use the frame count in the animation file
_animation = new MetaAnimation(fileName, _engine);
_frame = -1;
_frameDelay = 0;
} else if (line.matchString("*focus*", true)) {
_focused = true;
_engine->getScriptManager()->setFocusControlKey(_key);
}

line = stream.readLine();
Expand All @@ -94,51 +108,140 @@ InputControl::InputControl(ZVision *engine, uint32 key, Common::SeekableReadStre
}

bool InputControl::onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {
_engine->getScriptManager()->focusControl(_key);
if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
return false;

if (_textRectangle.contains(backgroundImageSpacePos)) {
if (!_readOnly) {
// Save
_engine->getScriptManager()->focusControl(_key);
} else {
// Restore
if (_currentInputText.size())
_enterPressed = true;
}
}
return false;
}

bool InputControl::onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos) {
if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
return false;

if (_textRectangle.contains(backgroundImageSpacePos)) {
if (!_readOnly) {
// Save
_engine->getCursorManager()->changeCursor(CursorIndex_Active);
return true;
} else {
// Restore
if (_currentInputText.size()) {
_engine->getCursorManager()->changeCursor(CursorIndex_Active);
_engine->getScriptManager()->focusControl(_key);
return true;
}
}
}
return false;
}

bool InputControl::onKeyDown(Common::KeyState keyState) {
if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
return false;

if (!_focused) {
return false;
}

if (keyState.keycode == Common::KEYCODE_BACKSPACE) {
_currentInputText.deleteLastChar();
if (!_readOnly) {
_currentInputText.deleteLastChar();
_textChanged = true;
}
} else if (keyState.keycode == Common::KEYCODE_RETURN) {
_enterPressed = true;
} else if (keyState.keycode == Common::KEYCODE_TAB) {
_focused = false;
unfocus();
// Focus the next input control
_engine->getScriptManager()->focusControl(_nextTabstop);
// Don't process this event for other controls
return true;
} else {
// Otherwise, append the new character to the end of the current text

uint16 asciiValue = keyState.ascii;
// We only care about text values
if (asciiValue >= 32 && asciiValue <= 126) {
_currentInputText += (char)asciiValue;
_textChanged = true;
if (!_readOnly) {
// Otherwise, append the new character to the end of the current text
uint16 asciiValue = keyState.ascii;
// We only care about text values
if (asciiValue >= 32 && asciiValue <= 126) {
_currentInputText += (char)asciiValue;
_textChanged = true;
}
}
}
return false;
}

bool InputControl::process(uint32 deltaTimeInMillis) {
if (!_focused) {
if (_engine->getScriptManager()->getStateFlag(_key) & Puzzle::DISABLED)
return false;
}

// First see if we need to render the text
if (_textChanged) {
// Blit the text using the RenderManager
//Common::Rect destRect = _engine->getRenderManager()->renderTextToWorkingWindow(_key, _currentInputText, _textStyle.font, _textRectangle.left, _textRectangle.top, _textStyle.color, _textRectangle.width());

//_cursorOffset = destRect.left - _textRectangle.left;
Graphics::Surface txt;
txt.create(_textRectangle.width(), _textRectangle.height(), _engine->_pixelFormat);

if (!_readOnly || !_focused)
_txtWidth = _engine->getTextRenderer()->drawTxt(_currentInputText, _string_init, txt);
else
_txtWidth = _engine->getTextRenderer()->drawTxt(_currentInputText, _string_chooser_init, txt);

_engine->getRenderManager()->blitSurfaceToBkg(txt, _textRectangle.left, _textRectangle.top);

txt.free();
}

if (_animation && !_readOnly && _focused) {
bool need_draw = true;// = _textChanged;
_frameDelay -= deltaTimeInMillis;
if (_frameDelay <= 0) {
_frame = (_frame + 1) % _animation->frameCount();
_frameDelay = _animation->frameTime();
need_draw = true;
}

if (need_draw) {
const Graphics::Surface *srf = _animation->getFrameData(_frame);
uint32 xx = _textRectangle.left + _txtWidth;
if (xx >= _textRectangle.left + (_textRectangle.width() - _animation->width()))
xx = _textRectangle.left + _textRectangle.width() - _animation->width();
_engine->getRenderManager()->blitSurfaceToBkg(*srf, xx, _textRectangle.top);
}
}

// Render the next frame of the animation
// TODO: Implement animation handling
_textChanged = false;
return false;
}

bool InputControl::enterPress() {
if (_enterPressed) {
_enterPressed = false;
return true;
}
return false;
}

void InputControl::setText(const Common::String &_str) {
_currentInputText = _str;
_textChanged = true;
}

const Common::String InputControl::getText() {
return _currentInputText;
}

void InputControl::setReadOnly(bool readonly) {
_readOnly = readonly;
}

} // End of namespace ZVision
20 changes: 18 additions & 2 deletions engines/zvision/scripting/controls/input_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#define ZVISION_INPUT_CONTROL_H

#include "zvision/scripting/control.h"
#include "zvision/animation/meta_animation.h"
#include "zvision/text/text.h"
#include "zvision/text/string_manager.h"

#include "common/rect.h"
Expand All @@ -38,25 +40,39 @@ class InputControl : public Control {
private:
Common::Rect _textRectangle;
Common::Rect _headerRectangle;
StringManager::TextStyle _textStyle;
cTxtStyle _string_init;
cTxtStyle _string_chooser_init;
uint32 _nextTabstop;
Common::String _cursorAnimationFileName;
bool _focused;

Common::String _currentInputText;
bool _textChanged;
uint _cursorOffset;
bool _enterPressed;
bool _readOnly;

int16 _txtWidth;
MetaAnimation *_animation;
int32 _frameDelay;
int16 _frame;

public:
void focus() {
_focused = true;
_textChanged = true;
}
void unfocus() {
_focused = false;
_textChanged = true;
}
bool onMouseUp(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos);
bool onMouseMove(const Common::Point &screenSpacePos, const Common::Point &backgroundImageSpacePos);
bool onKeyDown(Common::KeyState keyState);
bool process(uint32 deltaTimeInMillis);
void setText(const Common::String &_str);
const Common::String getText();
bool enterPress();
void setReadOnly(bool);
};

} // End of namespace ZVision
Expand Down
3 changes: 3 additions & 0 deletions engines/zvision/scripting/scr_file_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "zvision/scripting/controls/push_toggle_control.h"
#include "zvision/scripting/controls/lever_control.h"
#include "zvision/scripting/controls/slot_control.h"
#include "zvision/scripting/controls/input_control.h"

#include "common/textconsole.h"
#include "common/file.h"
Expand Down Expand Up @@ -346,6 +347,8 @@ Control *ScriptManager::parseControl(Common::String &line, Common::SeekableReadS
return new LeverControl(_engine, key, stream);
} else if (controlType.equalsIgnoreCase("slot")) {
return new SlotControl(_engine, key, stream);
} else if (controlType.equalsIgnoreCase("input")) {
return new InputControl(_engine, key, stream);
}
return NULL;
}
Expand Down

0 comments on commit 550c4db

Please sign in to comment.