Example #1
0
void ConsoleHandler::draw_all()
{
	Ego::Core::Console *console = egolib_console_top;

    if (!console)
    {
        return;
    }
	draw_begin();
    console->draw();
	draw_end();
}
Example #2
0
void egolib_console_handler_t::draw_all()
{
	Ego::Core::Console *console = egolib_console_top;

    if (!console)
    {
        return;
    }
	egolib_console_handler_t::draw_begin();
    console->draw();
    egolib_console_handler_t::draw_end();
}
Example #3
0
SDL_Event *ConsoleHandler::handle_event(SDL_Event *event)
{
	Ego::Core::Console *console = egolib_console_top;

	if (!event)
	{
		return nullptr;
	}
	if (!console)
	{
		return event;
	}

	// Only handle keyboard events.
	if (SDL_TEXTINPUT != event->type && SDL_TEXTEDITING != event->type && SDL_KEYDOWN != event->type)
		return event;

	SDL_Scancode vkey = SDL_SCANCODE_UNKNOWN;
	bool is_alt = false;
	bool is_shift = false;

	if (SDL_KEYDOWN == event->type)
	{
		// Grab the virtual scancode.
		vkey = event->key.keysym.scancode;

		// Get the key modifiers.
		SDL_Keymod kmod = SDL_GetModState();

		// Is alt or shift down?
		is_alt = HAS_SOME_BITS(kmod, KMOD_ALT | KMOD_CTRL);
		is_shift = HAS_SOME_BITS(kmod, KMOD_SHIFT);

		// If the virtual key code for the backquote is pressed,
		// toggle the console on the top of the console stack.
		if (!is_alt && !is_shift && (SDL_SCANCODE_GRAVE == vkey || (console->on && SDL_SCANCODE_ESCAPE == vkey)))
		{
			if (!console->on)
			{
				console->on = true;
				console->buffer_carat = 0;
				console->buffer[0] = CSTR_END;

				SDL_StartTextInput();
				return nullptr;
			}
			else
			{
				console->on = false;
				console->buffer_carat = 0;
				console->buffer[0] = CSTR_END;

				SDL_StopTextInput();
				return nullptr;
			}
		}
	}

	// Only grab the keycodes if the console is on.
	if (!console->on)
	{
		return event;
	}

	// Handle any console commands.
	if (SDL_KEYDOWN == event->type && !is_alt && !is_shift)
	{
		// backspace: delete character before the carat.
		if (SDL_SCANCODE_BACKSPACE == vkey)
		{
			while (console->buffer_carat > 0)
			{
				console->buffer_carat--;
				char a = console->buffer[console->buffer_carat];
				if ((a & 0x80) == 0x00 || (a & 0xC0) == 0xC0)
					break;
			}
			console->buffer[console->buffer_carat] = CSTR_END;

			event = nullptr;
		}
		else if (SDL_SCANCODE_UP == vkey)
		{
			console->history.up();
			strcpy(console->buffer,console->history.get_saved());
			console->buffer_carat = strlen(console->buffer);
			event = nullptr;
		}
		else if (SDL_SCANCODE_DOWN == vkey)
		{
			console->history.down();
			strcpy(console->buffer, console->history.get_saved());
			console->buffer_carat = strlen(console->buffer);
			event = nullptr;
		}
		else if (SDL_SCANCODE_LEFT == vkey)
		{
			console->buffer_carat--;
			console->buffer_carat = Ego::Math::constrain(console->buffer_carat, (size_t)0, (size_t)(ConsoleSettings::LineSettings::Length - 1));

			event = nullptr;
		}
		else if (SDL_SCANCODE_RIGHT == vkey)
		{
			console->buffer_carat++;
			console->buffer_carat = Ego::Math::constrain(console->buffer_carat, (size_t)0, (size_t)(ConsoleSettings::LineSettings::Length - 1));

			event = nullptr;
		}
		else if (SDL_SCANCODE_RETURN == vkey || SDL_SCANCODE_KP_ENTER == vkey)
		{
			console->buffer[console->buffer_carat] = CSTR_END;

			// Add this command line to the list of saved command line.
			console->history.add_saved(console->buffer);

			// Add the command line to the output buffer.
			console->print("%s %s\n", ConsoleSettings::InputSettings::Prompt.c_str(), console->buffer);

			// Actually execute the command line.
			console->run();

			// Blank the command line.
			console->buffer_carat = 0;
			console->buffer[0] = CSTR_END;

			event = nullptr;
		}
	}

	if (nullptr == event || SDL_KEYDOWN == event->type) return nullptr;

	bool addToLine = SDL_TEXTINPUT == event->type;
	char *text = SDL_TEXTINPUT == event->type ? event->text.text : event->edit.text;
	size_t textLength = strlen(text);

	// handle normal keystrokes
	if (console->buffer_carat + textLength + 1 < ConsoleSettings::LineSettings::Length)
	{
		strcat(console->buffer + console->buffer_carat, event->text.text);
		console->buffer[console->buffer_carat + textLength] = CSTR_END;
		if (addToLine)
			console->buffer_carat += strlen(event->text.text);

		event = nullptr;
	}

	return event;
}