building calendar activity using xamforms.controls.calendar - mvvm

So, like most, I am still pretty new and am navigating through all things c# and Xamarin.Forms, but it is going exceptionally slow. Using XamarinForms and the XamForms.Controls.Calendar plugin, I am trying to build a calendar that gets a LIST of dates and events that happened and puts a small color code at the bottom. I reviewed all of the source code and topics on this I could find and I have refactored it out and built a VERSION of the code below that does what I want but it is ugly...I want to make it more reuseable and more MVVM than the example they were using earlier and what I did. Essentially my plan is to bind colors to activity based on a bool value (should be able to see in the code). So, the challenge is that I don't really know where to go or what to do, but below is what I THINK needs to happen. Please forgive me if this is a simple question, I am just not seeing it.
XamlPage
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:XamForms.Controls;assembly=XamForms.Controls.Calendar"
x:Class="MyApp.View.LogPageViews.LogPage"
ControlTemplate="{StaticResource MainPageTemplate}">
<ContentPage.Content>
<ScrollView>
<StackLayout>
<controls:Calendar x:Name="calendar Padding="10,0,10,0" SelectedBorderWidth="4" DisabledBorderColor="Black" ShowNumberOfWeek="false" StartDay="Monday TitleRightArrowTextColor="Blue" TitleLabelTextColor="Purple" TitleLeftArrowTextColor="Blue" WeekdaysFontSize="12" SelectedDate="{Binding Date}" SpecialDates="{Binding Workout}" DateCommand="{Binding DateChosen}" >
</controls:Calendar>
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
I am getting my error in the code behind with everything I have tried...which is several probably wrong things...and I don't even know if this is good code. My current error is Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'MyApp.ViewModel.CalendarActivityVM' to 'XamForms.Controls.SpecialDate'
namespace MyApp.View.LogPageViews
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LogPage : ContentPage
{
public LogPage ()
{
InitializeComponent ();
var Dates = new List<SpecialDate>();
NavigationPage.SetHasNavigationBar(this, false);
DateTime testdate = new DateTime(2018, 06, 26);
//This is where I am getting the errors when I try to do anything like this. Getting errors like "Cannot convert from" and "Cannot use like a method"
//I did have code here that worked, but refactored it out and got stuck
calendar.SpecialDates.Add(new CalendarActivityVM(testdate, true, true, true, true));
calendar.SelectedDate = (DateTime.Now);
}
}
}
My Calendar ActivityVM (I have no idea if this is correct...This is where I am learning and think it is right)
namespace MyApp.ViewModel
{
public class CalendarActivityVM
{
public DateTime Datetime { get; set; }
public bool A { get; set; }
public bool B { get; set; }
public bool C { get; set; }
public bool D { get; set; }
public CalendarActivityVM(DateTime datetime, bool a, bool b, bool c, bool d)
{
Datetime = datetime;
A = a;
B = b;
C = c;
D = d;
var AColor = new Color();
var BColor = new Color();
var CColor = new Color();
var DColor = new Color();
if (A == true) { AColor = Color.Red; } else { AColor = Color.Transparent; };
if (B == true) { BColor = Color.Yellow; } else { BColor = Color.Transparent; };
if (C == true) { CColor = Color.Green; } else { CColor = Color.Transparent; };
if (D == true) { DColor = Color.Blue; } else { DColor = Color.Transparent; };
var white_row = new Pattern { WidthPercent = .1275f, HightPercent = 0.75f, Color = Color.Transparent };
var white_col = new Pattern { WidthPercent = 0.04f, HightPercent = 1f, Color = Color.Transparent };
var aPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = AColor };
var bPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = BColor };
var cPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = CColor };
var dPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = DColor };
var activity = new BackgroundPattern(7)
{
Pattern = new List<Pattern>
{
white_row, white_row, white_row, white_row, white_row, white_row, white_row,
aPattern, white_col, bPattern, white_col, cPattern, white_col, dPattern
}
};
}
}
}
a normal "SpecialDate" looks like this according to the example...this is why I am trying to refactor:
calendar.SpecialDates = new List<SpecialDate>{
new SpecialDate(DateTime.Now.AddDays(3))
{
BackgroundColor = Color.White,
TextColor = Color.Black,
Selectable = true,
BackgroundPattern = new BackgroundPattern(7)
{
Pattern = new List<Pattern>
{
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Red, Text = "X", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Gold, Text = "Y", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Green, Text = "Z", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Purple,Text = "Q", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
white_row,white_row,white_row,white_row,white_row,white_row,white_row,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Blue},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Chocolate},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Cyan},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Fuchsia},
white_row,white_row,white_row,white_row,white_row,white_row,white_row,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Crimson},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Aquamarine},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.OrangeRed},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.DarkOrchid},
white_row,white_row,white_row,white_row,white_row,white_row,white_row,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Black},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.DeepSkyBlue},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.DarkGoldenrod},
white_col,
new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Firebrick},
}
}
}
I appreciate your time and any help is greatly appreciated.

So, after a bit more research (and maybe this will help others) there is a really nice tool to refactor to a method that makes it much easier than trying to do it myself (although I had to play with it a couple of times before I got it correct). Using the example in the code (or the code I got to work), on the left side of the screen there is a little paintbrush (VS2017) that helped me refactor. Here is the code that I eventually generated after playing with it for just a couple minutes. Now I did create it in the codebehind, but now I am pretty certain I can refactor that to my VM.
private static SpecialDate TestMethod(DateTime datetime, bool A, bool B, bool C, bool D)
{
var AColor = new Color();
var BColor = new Color();
var CColor = new Color();
var DColor = new Color();
if (A == true) { AColor = Color.Red; } else { AColor = Color.Transparent; };
if (B == true) { BColor = Color.Yellow; } else { BColor = Color.Transparent; };
if (C == true) { CColor = Color.Green; } else { CColor = Color.Transparent; };
if (D == true) { DColor = Color.Blue; } else { DColor = Color.Transparent; };
var white_row = new Pattern { WidthPercent = .1275f, HightPercent = 0.75f, Color = Color.Transparent };
var white_col = new Pattern { WidthPercent = 0.04f, HightPercent = 1f, Color = Color.Transparent };
var aPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = AColor };
var bPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = BColor };
var cPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = CColor };
var dPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = DColor };
return new SpecialDate(datetime)
{
BackgroundColor = Color.White,
TextColor = Color.Black,
Selectable = true,
BackgroundPattern = new BackgroundPattern(7)
{
Pattern = new List<Pattern>
{
white_row, white_row, white_row, white_row, white_row, white_row, white_row,
aPattern, white_col, bPattern, white_col, cPattern, white_col, dPattern
}
}
};
}
I know I answered my own question and marked it as answered, but hopefully it will help someone else...showing how you can refactor much more simply than trying to do it on your own.

Related

How to prevent picked up object from changing its scale?

I'm facing an issue when I pick up an object in my game. Whenever I pick up an object and look around while holding the object, it stretches based on the perspective. Here is an example of an object before and after picking up:
Before picking up:
After picking up:
How can I maintain the object's scale and prevent it from stretching?
public void PickupObject()
{
physicsObject = lookObject.GetComponentInChildren<PhysicsObjects>();
currentlyPickedUpObject = lookObject;
pickupRB = currentlyPickedUpObject.GetComponent<Rigidbody>();
/* priorConstraints = pickupRB.constraints; // <--- NEW
pickupRB.constraints = RigidbodyConstraints.FreezeAll; // <--- NEW*/
pickupRB.constraints = RigidbodyConstraints.FreezeRotation;
physicsObject.playerInteractions = this;
pickupRB.isKinematic = true;
pickupRB.transform.parent = PickupParent.transform;
// pickupRB.isKinematic = true;
// StartCoroutine(physicsObject.PickUp());
}
The following code snippet is in Update() :
if (currentlyPickedUpObject != null)
{
HoldingItemIcon.SetActive(true);
InteractIcon.SetActive(false);
CenterIcon.SetActive(false);
currentDist = Vector3.Distance(PickupParent.position, pickupRB.position);
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, currentDist / maxDistance);
currentSpeed *= Time.fixedDeltaTime;
pickupRB.transform.position = PickupParent.position;
// pickupRB.transform.SetParent(PickupParent.transform);
Vector3 direction = PickupParent.position - pickupRB.position;
pickupRB.velocity = direction.normalized * currentSpeed;
Vector3 camerDirection = mainCamera.transform.forward;
// Throw object
if (Throw)
{
HoldingItemIcon.SetActive(false);
InteractIcon.SetActive(false);
pickupRB.constraints = RigidbodyConstraints.None;
pickupRB.isKinematic = false;
pickupRB.AddForce(camerDirection * 500);
currentlyPickedUpObject = null;
pickupRB.transform.parent = null;
}
Throw = false;
}
public void BreakConnection()
{
pickupRB.isKinematic = false;
pickupRB.transform.parent = null;
pickupRB.constraints = priorConstraints;
// pickupRB.constraints = RigidbodyConstraints.None;
currentlyPickedUpObject = null;
lookObject = null;
physicsObject.pickedUp = false;
currentDist = 0;
pickupRB.useGravity = true;
}
pickupParent's lossy scale while an object is picked:
What you can do is have an empty gameObject as a child of your player. That object is going to be the pickableObjectsParent and give this parent object the scale values of 1/(player's scale).

Unity3D Custom editor focus change with EditorGUILayout.LabelField

I've an issue working with custom editor, my editing focus is changed without any action from my part.
Basically, this is a screen of my custom editor in a neutral state (just added empty translation entity):
And this is a screen when my focus got change without reason. As you can see, my editor detected that 2 keys was the same and notify it by a message at the top of the box, the issue is that I was actually editing the second box's "key" field, and my focus changed on the first box (highlighted blue). If I don't display the warning message, I don't have any issue so I guess it comes from there..
This is my custom editor script :
[CustomEditor(typeof(InternationalizationDatabaseSO))]
public class InternationalizationDatabaseSOEditor : Editor
{
#region Constantes
private const string ITEMS_PROPERTY_NAME = "_mItems";
private const string ITEM_CATEGORY_NAME = "_mCategory";
private const string ITEM_KEY_NAME = "_mKey";
private const string ITEM_VALUES_NAME = "_mValues";
private const string ITEM_VALUE_LANGUAGE = "_mLanguage";
private const string ITEM_VALUE_VALUE = "_mValue";
#endregion
#region Attributs
private InternationalizationDatabaseSO _mDatabase;
#endregion
#region Methods
private void OnEnable()
{
Init();
}
private void Init()
{
_mDatabase = target as InternationalizationDatabaseSO;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty itemsProperty = serializedObject.FindProperty(ITEMS_PROPERTY_NAME);
int arraySize = itemsProperty.arraySize;
EditorGUI.BeginDisabledGroup(false);
{
EditorGUILayout.BeginHorizontal();
{
GUILayout.FlexibleSpace();
if (OnInspectorGUIButton("+", 40, 25, Color.white, Color.green))
{
itemsProperty.arraySize++;
itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1).FindPropertyRelative(ITEM_CATEGORY_NAME).stringValue = "";
itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1).FindPropertyRelative(ITEM_KEY_NAME).stringValue = "";
serializedObject.ApplyModifiedProperties();
Init();
return;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
for(int i = 0; i < arraySize; i++)
{
if(OnInspectorGUIItem(i) == false)
{
serializedObject.ApplyModifiedProperties();
Init();
return;
}
}
}
EditorGUI.EndDisabledGroup();
serializedObject.ApplyModifiedProperties();
}
private bool OnInspectorGUIItem(int index)
{
SerializedProperty itemsProperty = serializedObject.FindProperty(ITEMS_PROPERTY_NAME);
SerializedProperty itemCategory = itemsProperty.GetArrayElementAtIndex(index).FindPropertyRelative(ITEM_CATEGORY_NAME);
SerializedProperty itemKey = itemsProperty.GetArrayElementAtIndex(index).FindPropertyRelative(ITEM_KEY_NAME);
EditorGUI.indentLevel += 1;
EditorGUILayout.BeginVertical(GUI.skin.box);
{
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.BeginVertical();
{
if(KeyAlreadyExist(index, itemKey.stringValue))
{
OnInspectorGUIText("Key already exists", 12, Color.red, FontStyle.Bold, false);
}
OnInspectorGUIText("Key : " + itemKey.stringValue, 12, FontStyle.Normal, false);
}
EditorGUILayout.EndVertical();
GUILayout.FlexibleSpace();
if(OnInspectorGUIButton("-", 40, 25, Color.white, Color.red))
{
itemCategory.stringValue = "";
itemKey.stringValue = "";
itemsProperty.DeleteArrayElementAtIndex(index);
return false;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
{
GUIStyle style = EditorStyles.foldout;
style.fontSize = 15;
style.fontStyle = FontStyle.Bold;
itemCategory.isExpanded = EditorGUILayout.Foldout(itemCategory.isExpanded, "General informations", style);
if(itemCategory.isExpanded)
{
EditorGUILayout.Space();
EditorGUILayout.PropertyField(itemCategory, new GUIContent("Category"));
EditorGUILayout.PropertyField(itemKey, new GUIContent("Key"));
}
if(OnInspectorGUIItemLanguage(index, itemsProperty.GetArrayElementAtIndex(index)) == false)
{
return false;
}
}
EditorGUILayout.EndVertical();
}
EditorGUILayout.EndVertical();
EditorGUI.indentLevel -= 1;
return true;
}
private bool OnInspectorGUIItemLanguage(int index, SerializedProperty propertyItem)
{
EditorGUILayout.BeginVertical();
{
GUIStyle style = EditorStyles.foldout;
style.fontSize = 15;
style.normal.textColor = Color.black;
style.fontStyle = FontStyle.Bold;
SerializedProperty itemValues = propertyItem.FindPropertyRelative(ITEM_VALUES_NAME);
itemValues.isExpanded = EditorGUILayout.Foldout(itemValues.isExpanded, "Languages informations", style);
if(itemValues.isExpanded)
{
EditorGUILayout.Space();
EditorGUILayout.BeginVertical();
{
EditorGUILayout.BeginHorizontal();
{
int nbTranslation = itemValues.arraySize;
EditorGUILayout.LabelField("Nb translation : " + nbTranslation);
GUILayout.FlexibleSpace();
if (OnInspectorGUIButton("+", 20, 20, Color.white, Color.green))
{
itemValues.arraySize++;
itemValues.GetArrayElementAtIndex(itemValues.arraySize - 1).FindPropertyRelative(ITEM_VALUE_LANGUAGE).intValue = 0;
itemValues.GetArrayElementAtIndex(itemValues.arraySize - 1).FindPropertyRelative(ITEM_VALUE_VALUE).stringValue = "";
return false;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
if (itemValues.arraySize > 0)
{
string translatedLanguages = "{";
foreach (InternationalizationDatabaseItemLanguage item in _mDatabase.Items[index].Values)
{
translatedLanguages += item.Language.ToString() + ", ";
}
if (translatedLanguages.Length > 2)
{
translatedLanguages = translatedLanguages.Remove(translatedLanguages.Length - 2);
}
translatedLanguages += "}";
//EditorStyles.label.stretchHeight = true;
EditorStyles.label.wordWrap = true;
EditorGUILayout.LabelField("Translated : \n" + translatedLanguages);
EditorGUILayout.Space();
for (int i = 0; i < itemValues.arraySize; i++)
{
EditorGUILayout.BeginVertical(GUI.skin.box);
{
EditorGUILayout.Space();
InternationalizationDatabaseItemLanguage item = _mDatabase.Items[index].Values[i];
SerializedProperty propLanguage = itemValues.GetArrayElementAtIndex(i).FindPropertyRelative(ITEM_VALUE_LANGUAGE);
SerializedProperty propValue = itemValues.GetArrayElementAtIndex(i).FindPropertyRelative(ITEM_VALUE_VALUE);
EditorGUILayout.BeginHorizontal();
{
if (LanguageAlreadyExist(index, i, _mDatabase.Items[index].Values[i].Language))
{
OnInspectorGUIText("Warning language translation already exist", 12, Color.red, FontStyle.Bold, false);
}
GUILayout.FlexibleSpace();
if (OnInspectorGUIButton("-", 20, 20, Color.white, Color.red))
{
itemValues.DeleteArrayElementAtIndex(i);
return false;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.PropertyField(propLanguage, new GUIContent("Language"));
EditorGUILayout.PrefixLabel("Translation");
propValue.stringValue = EditorGUILayout.TextArea(propValue.stringValue, GUILayout.Height(80));
}
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
}
}
}
EditorGUILayout.EndVertical();
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Space();
return true;
}
private bool KeyAlreadyExist(int currentIndex, string key)
{
for(int i = 0; i < _mDatabase.Items.Count; i++)
{
InternationalizationDatabaseItem item = _mDatabase.Items[i];
if(currentIndex != i && item.Key == key && string.IsNullOrEmpty(key) == false)
{
return true;
}
}
return false;
}
private bool LanguageAlreadyExist(int indexInDatabase, int currentIndex, SystemLanguage language)
{
for (int i = 0; i < _mDatabase.Items[indexInDatabase].Values.Count; i++)
{
InternationalizationDatabaseItemLanguage item = _mDatabase.Items[indexInDatabase].Values[i];
if (currentIndex != i && item.Language == language)
{
return true;
}
}
return false;
}
private void OnInspectorGUIText(string text, FontStyle fontStyle, bool prefixLabel)
{
GUIStyle style = new GUIStyle();
style.fontStyle = fontStyle;
style.wordWrap = true;
if (prefixLabel)
{
EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
}
else
{
EditorGUILayout.LabelField(text, style);
}
}
private void OnInspectorGUIText(string text, int fontSize, FontStyle fontStyle, bool prefixLabel)
{
GUIStyle style = new GUIStyle();
style.fontSize = fontSize;
style.fontStyle = fontStyle;
style.wordWrap = true;
if (prefixLabel)
{
EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
}
else
{
EditorGUILayout.LabelField(text, style);
}
}
private void OnInspectorGUIText(string text, int fontSize, Color textColor, FontStyle fontStyle, bool prefixLabel)
{
GUIStyle style = new GUIStyle();
style.fontSize = fontSize;
style.normal.textColor = textColor;
style.fontStyle = fontStyle;
style.wordWrap = true;
if(prefixLabel)
{
EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
}
else
{
EditorGUILayout.LabelField(text, style);
}
}
private bool OnInspectorGUIButton(string label, int width, int height, Color textColor, Color backgroundColor)
{
Color saveColor = GUI.backgroundColor;
GUILayoutOption[] options = { GUILayout.Width(width), GUILayout.Height(height) };
GUIStyle style = new GUIStyle(GUI.skin.button);
style.normal.textColor = textColor;
GUI.backgroundColor = backgroundColor;
bool pressed = GUILayout.Button(label, style, options);
GUI.backgroundColor = saveColor;
return pressed;
}
#endregion
}
Have you any suggestion ? Thanks
Have a nice day.
Are you sure you are not sharing reference to the same property in both boxes ?
Ok finally figured it out, I mean, I'm not still totally certain why it happens, but found a workaround thanks to : https://forum.unity.com/threads/editor-gui-inputfield-loses-focus-when-gui-updates.542147/
Basically, I'm just displaying always the LabelField, but if there is no error, I simply set the errorMessage to an empty string

Xamarin form: Why there is a gap in between the cells

I have a custom view in side the item on the right highlighted in black.
The each view cell has a big gap because of the black custom view. This black view is a grid which was added in the bindingContextChanged function.
public partial class ShiftTemplate : ViewCell
{
public ShiftTemplate()
{
InitializeComponent();
}
}
public class ShiftView : ContentView
{
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
var model = BindingContext as StaffShiftViewModel;
var grid = new Grid();
var shiftCount = 0;
var index = 0;
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star });
foreach (var shift in model.Shifts)
{
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
var timeLabel = new Label
{
Text = shift.Time,
VerticalTextAlignment = TextAlignment.Center,
HorizontalTextAlignment = TextAlignment.End
};
var clockButton = new Button
{
Text = shift.ClockStatus,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start,
Command = shift.OnClockingPopupCommand
};
var binding = new Binding();
binding.Source = shift;
binding.Path = nameof(shift.ClockStatus);
binding.Mode = BindingMode.OneWay;
clockButton.SetBinding(Button.TextProperty, binding);
var line = new BoxView { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.Center, HeightRequest = 1, BackgroundColor = Color.Gray };
grid.Children.Add(timeLabel, 0, 1, shiftCount, shiftCount+1);
grid.Children.Add(clockButton, 1, 2, shiftCount, shiftCount+1);
index += 1;
if (index < model.Shifts.Count)
grid.Children.Add(line, 0, 2, shiftCount + 1, shiftCount + 2);
shiftCount+=2;
}
Content = grid;
ForceLayout();
}
}

Change yValue label vertical spacing in combined chart (ios-charts)

I have a combined chart that uses a line and bar chart to show yValues.
In some instances the line and bar chart values will overlap, is there a way to set the vertical spacing of the labels for the yValues so that they're not on top of each other (like Jan to Oct in the image)?
Combined Chart Image
I'm using the Charts framework (formerly ios-charts), here is the code to setup the CombineChartView:
let xValues = getXAxisLabelsForYear(year)
let runningTotalsByMonth = getRunningTotalByMonthForYear(year)!
var yValsBar = [BarChartDataEntry]()
var yValsLine = [ChartDataEntry]()
for i in 0 ..< xValues.count {
let yBarDataEntry = BarChartDataEntry(value: monthlyWinnings[i], xIndex: i)
yValsBar.append(yBarDataEntry)
let yLineDataEntry = ChartDataEntry(value: runningTotalsByMonth[i], xIndex: i)
yValsLine.append(yLineDataEntry)
}
let barChartDataSet = BarChartDataSet(yVals: yValsBar, label: "Monthly Winnings")
//setup bar chart
var barChartColors = [UIColor]()
for i in monthlyWinnings {
if i >= 0.0 {
barChartColors.append(myGreen)
} else {
barChartColors.append(UIColor.redColor())
}
}
barChartDataSet.colors = barChartColors
barChartDataSet.barShadowColor = UIColor.clearColor()
barChartDataSet.valueFont = UIFont.systemFontOfSize(10.0)
//setup line chart
let lineChartDataSet = LineChartDataSet(yVals: yValsLine, label: "Cumulative Winnings")
var lineChartColors = [UIColor]()
for i in runningTotalsByMonth {
if i >= 0.0 {
lineChartColors.append(myGreen)
} else {
lineChartColors.append(UIColor.redColor())
}
}
lineChartDataSet.colors = lineChartColors
lineChartDataSet.circleColors = [UIColor.blueColor()]
lineChartDataSet.drawCircleHoleEnabled = false
lineChartDataSet.circleRadius = 5
lineChartDataSet.lineWidth = 2
lineChartDataSet.valueFont = UIFont.systemFontOfSize(10.0)
//combine data
let data = CombinedChartData(xVals: xValues)
data.barData = BarChartData(xVals: xValues, dataSet: barChartDataSet)
data.lineData = LineChartData(xVals: xValues, dataSet: lineChartDataSet)
combinedChartView.data = data
//format the chart
combinedChartView.xAxis.setLabelsToSkip(0)
combinedChartView.xAxis.labelPosition = .Bottom
combinedChartView.descriptionText = ""
combinedChartView.rightAxis.drawLabelsEnabled = false
combinedChartView.rightAxis.drawGridLinesEnabled = false
combinedChartView.drawGridBackgroundEnabled = false
combinedChartView.leftAxis.drawZeroLineEnabled = true
combinedChartView.xAxis.drawGridLinesEnabled = false
combinedChartView.xAxis.wordWrapEnabled = true
You can draw bar chart values below the top of the bar using
chartView.drawValueAboveBarEnabled = false
and setting some color
barChartDataSet.valueTextColor = UIColor.someColor()
Will look like this:
See my comment above, but something like this may work if you're not using auto layout:
let labelA = UILabel()
let labelB = UILabel()
let padding: CGFloat = 5.0 // or whatever
if CGRectIntersectsRect(labelA.frame, labelB.frame) {
// If the minY of labelA is <= labelB's that means labelA is ABOVE labelB
if labelA.frame.minY <= labelB.frame.minY {
// Set it above, with some (optional) padding
labelA.frame.origin.y = labelB.frame.origin.y - padding - labelA.frame.height
} else {
labelB.frame.origin.y = labelA.frame.origin.y - padding - labelB.frame.height
}
}
Of course you'll need additional code for checking if it's too high and other edge cases.

Unity Fade delay when game ends

I'm fairly new at Unity and i'm trying to making a game.
I want to have an subtitle fading in when you're at the end of the game. This start when you hit a button.
But when I code a image that I fadein, it plays it directly when you start the game.
Do you guys know a solution?
#pragma strict
private var guiShow : boolean = false;
var car : GameObject;
var rayLength = 10;
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
var fadesubtitles : boolean = false;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "car")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
guiShow = false;
}
else if(Input.GetKeyDown("e"))
{
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 150, 25), "Press F to escape");
if(Input.GetKeyDown("f")){
fadesubtitles = true;
}
}
}
if (fadesubtitles == true){
yield FadeGUITexture(guiObject, fadeTime, Fade.In);
yield WaitForSeconds(3.0);
yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
if (subtitles == true){
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
}
I'd start your game object in the 'disabled' state (uncheck it in the inspector). Then at then end of the game, have some code that enables it.
You can use iTween.
FadeFrom(GameObject target, Hashtable args)
Example:
iTween.FadeFrom(gameObject, iTween.Hash("alpha", 0f, "amount", 1f, "time", 2f));