Skip to content

Instantly share code, notes, and snippets.

@SuperWangKai
Last active February 22, 2020 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuperWangKai/a07bebfee9738f817e9c79b8bf5302e1 to your computer and use it in GitHub Desktop.
Save SuperWangKai/a07bebfee9738f817e9c79b8bf5302e1 to your computer and use it in GitHub Desktop.
Path Monitoring Sample
//
// Copyright (c) 2008-2020 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <Urho3D/Core/CoreEvents.h>
#include <Urho3D/Core/ProcessUtils.h>
#include <Urho3D/Input/Input.h>
#include <Urho3D/UI/Font.h>
#include <Urho3D/UI/Text.h>
#include <Urho3D/UI/UI.h>
#include <Urho3D/Resource/ResourceEvents.h>
#include <Urho3D/IO/FileWatcher.h>
#include "HelloFileChange.h"
#include <Urho3D/DebugNew.h>
// Expands to this example's entry-point
URHO3D_DEFINE_APPLICATION_MAIN(HelloFileChange)
HelloFileChange::HelloFileChange(Context* context) :
Sample(context)
{
}
void HelloFileChange::Setup()
{
Sample::Setup();
engineParameters_[EP_WINDOW_WIDTH] = 1024;
engineParameters_[EP_WINDOW_HEIGHT] = 768;
engineParameters_[EP_WINDOW_RESIZABLE] = true;
}
void HelloFileChange::Start()
{
// Execute base class startup
Sample::Start();
GetSubsystem<ResourceCache>()->SetAutoReloadResources(true, true);
// Create "Hello World" Text
CreateText();
// Set the mouse mode to use in the sample
Sample::InitMouseMode(MM_FREE);
// Here we start the path monitoring
StartPathMonitor();
}
void HelloFileChange::StartPathMonitor()
{
SubscribeToEvent(E_FILECHANGED, URHO3D_HANDLER(HelloFileChange, HandleFileChanged));
}
void HelloFileChange::StopPathMonitor()
{
changeLog_.Clear();
UnsubscribeFromEvent(E_FILECHANGED);
}
void HelloFileChange::Stop()
{
Sample::Stop();
StopPathMonitor();
}
void HelloFileChange::CreateText()
{
auto* cache = GetSubsystem<ResourceCache>();
// Construct new Text object
text_ = new Text(context_);
// Set String to display
text_->SetText("Hello File Change! Modify any file or folder under \"Data\" to test.");
// Set font and text color
text_->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 10);
text_->SetColor(Color(0.0f, 1.0f, 0.0f));
// Align Text
text_->SetHorizontalAlignment(HA_RIGHT);
text_->SetVerticalAlignment(VA_TOP);
// Add Text instance to the UI root element
GetSubsystem<UI>()->GetRoot()->AddChild(text_);
}
String GetFileChangeName(FileChangeType type)
{
switch (type)
{
case FILECHANGE_UNKNOWN: return "Unknown";
case FILECHANGE_ADDED: return "Added";
case FILECHANGE_MODIFIED: return "Modified";
case FILECHANGE_REMOVED: return "Removed";
default: return "Invalid";
}
}
void HelloFileChange::HandleFileChanged(StringHash eventType, VariantMap& eventData)
{
using namespace FileChanged;
const String& resourceName = eventData[P_RESOURCENAME].GetString();
int changedType = eventData[P_CHANGETYPE].GetInt();
const String& fileName = eventData[P_FILENAME].GetString();
String info = "File changed event: FileName=" + fileName +
", ResouceName=" + resourceName + ", ChangedType=" +
GetFileChangeName((FileChangeType)changedType) + "\n";
#ifdef URHO3D_LOGGING
URHO3D_LOGDEBUG(info);
#endif
changeLog_.Append(info);
assert(text_);
if (text_)
{
text_->SetText(changeLog_);
}
}
//
// Copyright (c) 2008-2020 the Urho3D project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#pragma once
#include "Sample.h"
namespace Urho3D
{
class Text;
}
class HelloFileChange : public Sample
{
URHO3D_OBJECT(HelloFileChange, Sample);
public:
/// Construct.
explicit HelloFileChange(Context* context);
/// Setup before engine initialization. Modifies the engine parameters.
void Setup() override;
/// Setup after engine initialization and before running the main loop.
void Start() override;
/// Cleanup after the main loop. Called by Application.
void Stop() override;
/// Start path monitoring
void StartPathMonitor();
/// Stop path monitoring
void StopPathMonitor();
protected:
/// Return XML patch instructions for screen joystick layout for a specific sample app, if any.
String GetScreenJoystickPatchString() const override { return
"<patch>"
" <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">"
" <attribute name=\"Is Visible\" value=\"false\" />"
" </add>"
"</patch>";
}
private:
/// Construct a new Text instance, containing the 'Hello World' String, and add it to the UI root element.
void CreateText();
/// Handle file change event.
void HandleFileChanged(StringHash eventType, VariantMap& eventData);
private:
/// File watcher path monitoring.
String changeLog_;
SharedPtr<Text> text_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment