sources/interface.h

Wed, 27 Jan 2021 18:55:14 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 18:55:14 +0200
changeset 187
53f9c7b2c068
parent 186
9330b93d9946
child 189
248d0b85cbda
permissions
-rw-r--r--

fixed an off-by-one error

/*
	Copyright 2014 - 2016 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once
#include "main.h"
#include "network/ipaddress.h"
#include "coloredline.h"
#include "network/rconsession.h"
#include "coloredline.h"

BEGIN_ZFC_NAMESPACE

class Interface
{
public:
	enum InputState
	{
		INPUTSTATE_NORMAL,
		INPUTSTATE_ADDRESS,
		INPUTSTATE_PASSWORD,
		INPUTSTATE_CONFIRM_DISCONNECTION,
	};

	Interface();
	void connect(std::string address, std::string password);
	void disconnected();
	RCONSession* getSession() { return &m_session; }
	void handleCommand(const std::string& input);
	void handleInput();
	void needRefresh();
	void __cdecl print(const char* fmtstr, ...);
	void __cdecl printWarning(const char* fmtstr, ...);
	void __cdecl printError(const char* fmtstr, ...);
	void __cdecl printText(const char* fmtstr, ...);
	void render();
	void renderFull();
	void setPlayerNames(const std::vector<std::string>& names);
	void setTitle(const std::string& message);
	void tabComplete(const std::string& part, std::string complete);
	void updateStatusBar();
	void vprint(const char* fmtstr, va_list args);

private:
	std::vector<std::string> m_inputHistory;
	int m_inputCursor;
	int m_cursorPosition;
	int m_inputPanning;
	bool m_needRefresh;
	bool m_needStatusBarRender;
	bool m_needInputRender;
	bool m_needOutputRender;
	bool m_needNicklistRender;
	struct { char ch; int x; } m_cursorCharacter;
	Vector<ColoredLine> m_outputLines;
	int m_outputScroll;
	std::string m_title;
	InputState m_inputState;
	std::function<void(bool)> m_disconnectCallback;
	net::ip_address m_remoteAddress;
	std::string m_statusBarText;
	std::vector<ColoredLine> m_playerNames;
	std::string m_pasteBuffer;
	RCONSession m_session;

	void detachInput();
	int findNextWord();
	int findPreviousWord();
	void flushInput();
	chtype getColorPair(Color fg, Color bg);
	const std::string& getCurrentInput();
	std::string& getEditableInput();
	std::string getPromptString();
	void moveInputCursor(int delta);
	int nicklistWidth();
	void positionCursor();
	void printToConsole(std::string message);
	int renderColorline(int y, int x0, int width, const ColoredLine& line, bool allowWrap);
	void renderInput();
	void renderNicklist();
	void renderOutput();
	void renderStatusBar();
	void renderTitlebar();
	void resetTitle();
	void safeDisconnect(std::function<void(bool)> afterwards);
	void setInputState(InputState newstate);
	void yank(int a, int b);
	bool tryResolveAddress(const std::string& address_string, net::ip_address *target);
};

END_ZFC_NAMESPACE

mercurial